1/* Subdriver for the GL860 chip with the OV9655 sensor
2 * Author Olivier LORIN, from logs done by Simon (Sur3) and Almighurt
3 * on dsd's weblog
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/* Sensor : OV9655 */
20
21#include "gl860.h"
22
23static struct validx tbl_init_at_startup[] = {
24	{0x0000, 0x0000}, {0x0010, 0x0010}, {0x0008, 0x00c0}, {0x0001, 0x00c1},
25	{0x0001, 0x00c2}, {0x0020, 0x0006}, {0x006a, 0x000d},
26
27	{0x0040, 0x0000},
28};
29
30static struct validx tbl_commmon[] = {
31	{0x0041, 0x0000}, {0x006a, 0x0007}, {0x0063, 0x0006}, {0x006a, 0x000d},
32	{0x0000, 0x00c0}, {0x0010, 0x0010}, {0x0001, 0x00c1}, {0x0041, 0x00c2},
33	{0x0004, 0x00d8}, {0x0012, 0x0004}, {0x0000, 0x0058}, {0x0040, 0x0000},
34	{0x00f3, 0x0006}, {0x0058, 0x0000}, {0x0048, 0x0000}, {0x0061, 0x0000},
35};
36
37static s32 tbl_length[] = {12, 56, 52, 54, 56, 42, 32, 12};
38
39static u8 *tbl_640[] = {
40	"\x00\x40\x07\x6a\x06\xf3\x0d\x6a" "\x10\x10\xc1\x01"
41	,
42	"\x12\x80\x00\x00\x01\x98\x02\x80" "\x03\x12\x04\x03\x0b\x57\x0e\x61"
43	"\x0f\x42\x11\x01\x12\x60\x13\x00" "\x14\x3a\x16\x24\x17\x14\x18\x00"
44	"\x19\x01\x1a\x3d\x1e\x04\x24\x3c" "\x25\x36\x26\x72\x27\x08\x28\x08"
45	"\x29\x15\x2a\x00\x2b\x00\x2c\x08"
46	,
47	"\x32\xff\x33\x00\x34\x3d\x35\x00" "\x36\xfa\x38\x72\x39\x57\x3a\x00"
48	"\x3b\x0c\x3d\x99\x3e\x0c\x3f\xc1" "\x40\xc0\x41\x00\x42\xc0\x43\x0a"
49	"\x44\xf0\x45\x46\x46\x62\x47\x2a" "\x48\x3c\x4a\xee\x4b\xe7\x4c\xe7"
50	"\x4d\xe7\x4e\xe7"
51	,
52	"\x4f\x98\x50\x98\x51\x00\x52\x28" "\x53\x70\x54\x98\x58\x1a\x59\x85"
53	"\x5a\xa9\x5b\x64\x5c\x84\x5d\x53" "\x5e\x0e\x5f\xf0\x60\xf0\x61\xf0"
54	"\x62\x00\x63\x00\x64\x02\x65\x20" "\x66\x00\x69\x0a\x6b\x5a\x6c\x04"
55	"\x6d\x55\x6e\x00\x6f\x9d"
56	,
57	"\x70\x15\x71\x78\x72\x00\x73\x00" "\x74\x3a\x75\x35\x76\x01\x77\x02"
58	"\x7a\x24\x7b\x04\x7c\x07\x7d\x10" "\x7e\x28\x7f\x36\x80\x44\x81\x52"
59	"\x82\x60\x83\x6c\x84\x78\x85\x8c" "\x86\x9e\x87\xbb\x88\xd2\x89\xe5"
60	"\x8a\x23\x8c\x8d\x90\x7c\x91\x7b"
61	,
62	"\x9d\x02\x9e\x02\x9f\x74\xa0\x73" "\xa1\x40\xa4\x50\xa5\x68\xa6\x70"
63	"\xa8\xc1\xa9\xef\xaa\x92\xab\x04" "\xac\x80\xad\x80\xae\x80\xaf\x80"
64	"\xb2\xf2\xb3\x20\xb4\x20\xb5\x00" "\xb6\xaf"
65	,
66	"\xbb\xae\xbc\x4f\xbd\x4e\xbe\x6a" "\xbf\x68\xc0\xaa\xc1\xc0\xc2\x01"
67	"\xc3\x4e\xc6\x85\xc7\x81\xc9\xe0" "\xca\xe8\xcb\xf0\xcc\xd8\xcd\x93"
68	,
69	"\xd0\x01\xd1\x08\xd2\xe0\xd3\x01" "\xd4\x10\xd5\x80"
70};
71
72static u8 *tbl_1280[] = {
73	"\x00\x40\x07\x6a\x06\xf3\x0d\x6a" "\x10\x10\xc1\x01"
74	,
75	"\x12\x80\x00\x00\x01\x98\x02\x80" "\x03\x12\x04\x01\x0b\x57\x0e\x61"
76	"\x0f\x42\x11\x00\x12\x00\x13\x00" "\x14\x3a\x16\x24\x17\x1b\x18\xbb"
77	"\x19\x01\x1a\x81\x1e\x04\x24\x3c" "\x25\x36\x26\x72\x27\x08\x28\x08"
78	"\x29\x15\x2a\x00\x2b\x00\x2c\x08"
79	,
80	"\x32\xa4\x33\x00\x34\x3d\x35\x00" "\x36\xf8\x38\x72\x39\x57\x3a\x00"
81	"\x3b\x0c\x3d\x99\x3e\x0c\x3f\xc2" "\x40\xc0\x41\x00\x42\xc0\x43\x0a"
82	"\x44\xf0\x45\x46\x46\x62\x47\x2a" "\x48\x3c\x4a\xec\x4b\xe8\x4c\xe8"
83	"\x4d\xe8\x4e\xe8"
84	,
85	"\x4f\x98\x50\x98\x51\x00\x52\x28" "\x53\x70\x54\x98\x58\x1a\x59\x85"
86	"\x5a\xa9\x5b\x64\x5c\x84\x5d\x53" "\x5e\x0e\x5f\xf0\x60\xf0\x61\xf0"
87	"\x62\x00\x63\x00\x64\x02\x65\x20" "\x66\x00\x69\x02\x6b\x5a\x6c\x04"
88	"\x6d\x55\x6e\x00\x6f\x9d"
89	,
90	"\x70\x08\x71\x78\x72\x00\x73\x01" "\x74\x3a\x75\x35\x76\x01\x77\x02"
91	"\x7a\x24\x7b\x04\x7c\x07\x7d\x10" "\x7e\x28\x7f\x36\x80\x44\x81\x52"
92	"\x82\x60\x83\x6c\x84\x78\x85\x8c" "\x86\x9e\x87\xbb\x88\xd2\x89\xe5"
93	"\x8a\x23\x8c\x0d\x90\x90\x91\x90"
94	,
95	"\x9d\x02\x9e\x02\x9f\x94\xa0\x94" "\xa1\x01\xa4\x50\xa5\x68\xa6\x70"
96	"\xa8\xc1\xa9\xef\xaa\x92\xab\x04" "\xac\x80\xad\x80\xae\x80\xaf\x80"
97	"\xb2\xf2\xb3\x20\xb4\x20\xb5\x00" "\xb6\xaf"
98	,
99	"\xbb\xae\xbc\x38\xbd\x39\xbe\x01" "\xbf\x01\xc0\xe2\xc1\xc0\xc2\x01"
100	"\xc3\x4e\xc6\x85\xc7\x81\xc9\xe0" "\xca\xe8\xcb\xf0\xcc\xd8\xcd\x93"
101	,
102	"\xd0\x21\xd1\x18\xd2\xe0\xd3\x01" "\xd4\x28\xd5\x00"
103};
104
105static u8 c04[] = {0x04};
106static u8 dat_post1[] = "\x04\x00\x10\x20\xa1\x00\x00\x02";
107static u8 dat_post2[] = "\x10\x10\xc1\x02";
108static u8 dat_post3[] = "\x04\x00\x10\x7c\xa1\x00\x00\x04";
109static u8 dat_post4[] = "\x10\x02\xc1\x06";
110static u8 dat_post5[] = "\x04\x00\x10\x7b\xa1\x00\x00\x08";
111static u8 dat_post6[] = "\x10\x10\xc1\x05";
112static u8 dat_post7[] = "\x04\x00\x10\x7c\xa1\x00\x00\x08";
113static u8 dat_post8[] = "\x04\x00\x10\x7c\xa1\x00\x00\x09";
114
115static struct validx tbl_init_post_alt[] = {
116	{0x6032, 0x00ff}, {0x6032, 0x00ff}, {0x6032, 0x00ff}, {0x603c, 0x00ff},
117	{0x6003, 0x00ff}, {0x6032, 0x00ff}, {0x6032, 0x00ff}, {0x6001, 0x00ff},
118	{0x6000, 0x801e},
119	{0xffff, 0xffff},
120	{0x6004, 0x001e}, {0x6000, 0x801e},
121	{0xffff, 0xffff},
122	{0x6004, 0x001e}, {0x6012, 0x0003}, {0x6000, 0x801e},
123	{0xffff, 0xffff},
124	{0x6004, 0x001e}, {0x6000, 0x801e},
125	{0xffff, 0xffff},
126	{0x6004, 0x001e}, {0x6012, 0x0003},
127	{0xffff, 0xffff},
128	{0x6000, 0x801e},
129	{0xffff, 0xffff},
130	{0x6004, 0x001e}, {0x6000, 0x801e},
131	{0xffff, 0xffff},
132	{0x6004, 0x001e}, {0x6012, 0x0003}, {0x6000, 0x801e},
133	{0xffff, 0xffff},
134	{0x6004, 0x001e}, {0x6000, 0x801e},
135	{0xffff, 0xffff},
136	{0x6004, 0x001e}, {0x6012, 0x0003},
137	{0xffff, 0xffff},
138	{0x6000, 0x801e},
139	{0xffff, 0xffff},
140	{0x6004, 0x001e}, {0x6000, 0x801e},
141	{0xffff, 0xffff},
142	{0x6004, 0x001e}, {0x6012, 0x0003},
143};
144
145static int  ov9655_init_at_startup(struct gspca_dev *gspca_dev);
146static int  ov9655_configure_alt(struct gspca_dev *gspca_dev);
147static int  ov9655_init_pre_alt(struct gspca_dev *gspca_dev);
148static int  ov9655_init_post_alt(struct gspca_dev *gspca_dev);
149static void ov9655_post_unset_alt(struct gspca_dev *gspca_dev);
150static int  ov9655_camera_settings(struct gspca_dev *gspca_dev);
151/*==========================================================================*/
152
153void ov9655_init_settings(struct gspca_dev *gspca_dev)
154{
155	struct sd *sd = (struct sd *) gspca_dev;
156
157	sd->vcur.backlight  =   0;
158	sd->vcur.brightness = 128;
159	sd->vcur.sharpness  =   0;
160	sd->vcur.contrast   =   0;
161	sd->vcur.gamma      =   0;
162	sd->vcur.hue        =   0;
163	sd->vcur.saturation =   0;
164	sd->vcur.whitebal   =   0;
165
166	sd->vmax.backlight  =   0;
167	sd->vmax.brightness = 255;
168	sd->vmax.sharpness  =   0;
169	sd->vmax.contrast   =   0;
170	sd->vmax.gamma      =   0;
171	sd->vmax.hue        =   0 + 1;
172	sd->vmax.saturation =   0;
173	sd->vmax.whitebal   =   0;
174	sd->vmax.mirror     = 0;
175	sd->vmax.flip       = 0;
176	sd->vmax.AC50Hz     = 0;
177
178	sd->dev_camera_settings = ov9655_camera_settings;
179	sd->dev_init_at_startup = ov9655_init_at_startup;
180	sd->dev_configure_alt   = ov9655_configure_alt;
181	sd->dev_init_pre_alt    = ov9655_init_pre_alt;
182	sd->dev_post_unset_alt  = ov9655_post_unset_alt;
183}
184
185/*==========================================================================*/
186
187static int ov9655_init_at_startup(struct gspca_dev *gspca_dev)
188{
189	fetch_validx(gspca_dev, tbl_init_at_startup,
190			ARRAY_SIZE(tbl_init_at_startup));
191	fetch_validx(gspca_dev, tbl_commmon, ARRAY_SIZE(tbl_commmon));
192/*	ctrl_out(gspca_dev, 0x40, 11, 0x0000, 0x0000, 0, NULL);*/
193
194	return 0;
195}
196
197static int ov9655_init_pre_alt(struct gspca_dev *gspca_dev)
198{
199	struct sd *sd = (struct sd *) gspca_dev;
200
201	sd->vold.brightness = -1;
202	sd->vold.hue = -1;
203
204	fetch_validx(gspca_dev, tbl_commmon, ARRAY_SIZE(tbl_commmon));
205
206	ov9655_init_post_alt(gspca_dev);
207
208	return 0;
209}
210
211static int ov9655_init_post_alt(struct gspca_dev *gspca_dev)
212{
213	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
214	s32 n; /* reserved for FETCH functions */
215	s32 i;
216	u8 **tbl;
217
218	ctrl_out(gspca_dev, 0x40, 5, 0x0001, 0x0000, 0, NULL);
219
220	tbl = (reso == IMAGE_640) ? tbl_640 : tbl_1280;
221
222	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200,
223			tbl_length[0], tbl[0]);
224	for (i = 1; i < 7; i++)
225		ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200,
226				tbl_length[i], tbl[i]);
227	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200,
228			tbl_length[7], tbl[7]);
229
230	n = fetch_validx(gspca_dev, tbl_init_post_alt,
231			ARRAY_SIZE(tbl_init_post_alt));
232
233	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
234	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
235					ARRAY_SIZE(tbl_init_post_alt), n);
236	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
237	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
238					ARRAY_SIZE(tbl_init_post_alt), n);
239	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
240	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
241					ARRAY_SIZE(tbl_init_post_alt), n);
242	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
243	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
244					ARRAY_SIZE(tbl_init_post_alt), n);
245	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post1);
246	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
247					ARRAY_SIZE(tbl_init_post_alt), n);
248
249	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
250	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
251					ARRAY_SIZE(tbl_init_post_alt), n);
252	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
253	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
254					ARRAY_SIZE(tbl_init_post_alt), n);
255	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
256	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
257					ARRAY_SIZE(tbl_init_post_alt), n);
258	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
259	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
260					ARRAY_SIZE(tbl_init_post_alt), n);
261	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post1);
262	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
263					ARRAY_SIZE(tbl_init_post_alt), n);
264
265	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
266	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
267					ARRAY_SIZE(tbl_init_post_alt), n);
268	ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x801e, 1, c04);
269	keep_on_fetching_validx(gspca_dev, tbl_init_post_alt,
270					ARRAY_SIZE(tbl_init_post_alt), n);
271
272	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post1);
273
274	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 4, dat_post2);
275	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post3);
276
277	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 4, dat_post4);
278	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post5);
279
280	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 4, dat_post6);
281	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post7);
282
283	ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_post8);
284
285	ov9655_camera_settings(gspca_dev);
286
287	return 0;
288}
289
290static int ov9655_configure_alt(struct gspca_dev *gspca_dev)
291{
292	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
293
294	switch (reso) {
295	case IMAGE_640:
296		gspca_dev->alt = 1 + 1;
297		break;
298
299	default:
300		gspca_dev->alt = 1 + 1;
301		break;
302	}
303	return 0;
304}
305
306static int ov9655_camera_settings(struct gspca_dev *gspca_dev)
307{
308	struct sd *sd = (struct sd *) gspca_dev;
309
310	u8 dat_bright[] = "\x04\x00\x10\x7c\xa1\x00\x00\x70";
311
312	s32 bright = sd->vcur.brightness;
313	s32 hue    = sd->vcur.hue;
314
315	if (bright != sd->vold.brightness) {
316		sd->vold.brightness = bright;
317		if (bright < 0 || bright > sd->vmax.brightness)
318			bright = 0;
319
320		dat_bright[3] = bright;
321		ctrl_out(gspca_dev, 0x40, 3, 0x6000, 0x0200, 8, dat_bright);
322	}
323
324	if (hue != sd->vold.hue) {
325		sd->vold.hue = hue;
326		sd->swapRB = (hue != 0);
327	}
328
329	return 0;
330}
331
332static void ov9655_post_unset_alt(struct gspca_dev *gspca_dev)
333{
334	ctrl_out(gspca_dev, 0x40, 5, 0x0000, 0x0000, 0, NULL);
335	ctrl_out(gspca_dev, 0x40, 1, 0x0061, 0x0000, 0, NULL);
336}
337