18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/* Subdriver for the GL860 chip with the OV2640 sensor
38c2ecf20Sopenharmony_ci * Author Olivier LORIN, from Malmostoso's logs
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/* Sensor : OV2640 */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "gl860.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_cistatic u8 dat_init1[] = "\x00\x41\x07\x6a\x06\x61\x0d\x6a" "\x10\x10\xc1\x01";
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic u8 c61[] = {0x61}; /* expected */
138c2ecf20Sopenharmony_cistatic u8 c51[] = {0x51}; /* expected */
148c2ecf20Sopenharmony_cistatic u8 c50[] = {0x50}; /* expected */
158c2ecf20Sopenharmony_cistatic u8 c28[] = {0x28}; /* expected */
168c2ecf20Sopenharmony_cistatic u8 ca8[] = {0xa8}; /* expected */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic u8 dat_post[] =
198c2ecf20Sopenharmony_ci	"\x00\x41\x07\x6a\x06\xef\x0d\x6a" "\x10\x10\xc1\x01";
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic u8 dat_640[]  = "\xd0\x01\xd1\x08\xd2\xe0\xd3\x02\xd4\x10\xd5\x81";
228c2ecf20Sopenharmony_cistatic u8 dat_800[]  = "\xd0\x01\xd1\x10\xd2\x58\xd3\x02\xd4\x18\xd5\x21";
238c2ecf20Sopenharmony_cistatic u8 dat_1280[] = "\xd0\x01\xd1\x18\xd2\xc0\xd3\x02\xd4\x28\xd5\x01";
248c2ecf20Sopenharmony_cistatic u8 dat_1600[] = "\xd0\x01\xd1\x20\xd2\xb0\xd3\x02\xd4\x30\xd5\x41";
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic struct validx tbl_init_at_startup[] = {
278c2ecf20Sopenharmony_ci	{0x0000, 0x0000}, {0x0010, 0x0010}, {0x0008, 0x00c0}, {0x0001, 0x00c1},
288c2ecf20Sopenharmony_ci	{0x0001, 0x00c2}, {0x0020, 0x0006}, {0x006a, 0x000d},
298c2ecf20Sopenharmony_ci	{0x0050, 0x0000}, {0x0041, 0x0000}, {0x006a, 0x0007}, {0x0061, 0x0006},
308c2ecf20Sopenharmony_ci	{0x006a, 0x000d}, {0x0000, 0x00c0}, {0x0010, 0x0010}, {0x0001, 0x00c1},
318c2ecf20Sopenharmony_ci	{0x0041, 0x00c2}, {0x0004, 0x00d8}, {0x0012, 0x0004}, {0x0000, 0x0058},
328c2ecf20Sopenharmony_ci	{0x0041, 0x0000}, {0x0061, 0x0000},
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic struct validx tbl_common[] = {
368c2ecf20Sopenharmony_ci	{0x6000, 0x00ff}, {0x60ff, 0x002c}, {0x60df, 0x002e}, {0x6001, 0x00ff},
378c2ecf20Sopenharmony_ci	{0x6080, 0x0012}, {0x6000, 0x0000}, {0x6000, 0x0045}, {0x6000, 0x0010},
388c2ecf20Sopenharmony_ci	{0x6035, 0x003c}, {0x6000, 0x0011}, {0x6028, 0x0004}, {0x60e5, 0x0013},
398c2ecf20Sopenharmony_ci	{0x6088, 0x0014}, {0x600c, 0x002c}, {0x6078, 0x0033}, {0x60f7, 0x003b},
408c2ecf20Sopenharmony_ci	{0x6000, 0x003e}, {0x6011, 0x0043}, {0x6010, 0x0016}, {0x6082, 0x0039},
418c2ecf20Sopenharmony_ci	{0x6088, 0x0035}, {0x600a, 0x0022}, {0x6040, 0x0037}, {0x6000, 0x0023},
428c2ecf20Sopenharmony_ci	{0x60a0, 0x0034}, {0x601a, 0x0036}, {0x6002, 0x0006}, {0x60c0, 0x0007},
438c2ecf20Sopenharmony_ci	{0x60b7, 0x000d}, {0x6001, 0x000e}, {0x6000, 0x004c}, {0x6081, 0x004a},
448c2ecf20Sopenharmony_ci	{0x6099, 0x0021}, {0x6002, 0x0009}, {0x603e, 0x0024}, {0x6034, 0x0025},
458c2ecf20Sopenharmony_ci	{0x6081, 0x0026}, {0x6000, 0x0000}, {0x6000, 0x0045}, {0x6000, 0x0010},
468c2ecf20Sopenharmony_ci	{0x6000, 0x005c}, {0x6000, 0x0063}, {0x6000, 0x007c}, {0x6070, 0x0061},
478c2ecf20Sopenharmony_ci	{0x6080, 0x0062}, {0x6080, 0x0020}, {0x6030, 0x0028}, {0x6000, 0x006c},
488c2ecf20Sopenharmony_ci	{0x6000, 0x006e}, {0x6002, 0x0070}, {0x6094, 0x0071}, {0x60c1, 0x0073},
498c2ecf20Sopenharmony_ci	{0x6034, 0x003d}, {0x6057, 0x005a}, {0x60bb, 0x004f}, {0x609c, 0x0050},
508c2ecf20Sopenharmony_ci	{0x6080, 0x006d}, {0x6002, 0x0039}, {0x6033, 0x003a}, {0x60f1, 0x003b},
518c2ecf20Sopenharmony_ci	{0x6031, 0x003c}, {0x6000, 0x00ff}, {0x6014, 0x00e0}, {0x60ff, 0x0076},
528c2ecf20Sopenharmony_ci	{0x60a0, 0x0033}, {0x6020, 0x0042}, {0x6018, 0x0043}, {0x6000, 0x004c},
538c2ecf20Sopenharmony_ci	{0x60d0, 0x0087}, {0x600f, 0x0088}, {0x6003, 0x00d7}, {0x6010, 0x00d9},
548c2ecf20Sopenharmony_ci	{0x6005, 0x00da}, {0x6082, 0x00d3}, {0x60c0, 0x00f9}, {0x6006, 0x0044},
558c2ecf20Sopenharmony_ci	{0x6007, 0x00d1}, {0x6002, 0x00d2}, {0x6000, 0x00d2}, {0x6011, 0x00d8},
568c2ecf20Sopenharmony_ci	{0x6008, 0x00c8}, {0x6080, 0x00c9}, {0x6008, 0x007c}, {0x6020, 0x007d},
578c2ecf20Sopenharmony_ci	{0x6020, 0x007d}, {0x6000, 0x0090}, {0x600e, 0x0091}, {0x601a, 0x0091},
588c2ecf20Sopenharmony_ci	{0x6031, 0x0091}, {0x605a, 0x0091}, {0x6069, 0x0091}, {0x6075, 0x0091},
598c2ecf20Sopenharmony_ci	{0x607e, 0x0091}, {0x6088, 0x0091}, {0x608f, 0x0091}, {0x6096, 0x0091},
608c2ecf20Sopenharmony_ci	{0x60a3, 0x0091}, {0x60af, 0x0091}, {0x60c4, 0x0091}, {0x60d7, 0x0091},
618c2ecf20Sopenharmony_ci	{0x60e8, 0x0091}, {0x6020, 0x0091}, {0x6000, 0x0092}, {0x6006, 0x0093},
628c2ecf20Sopenharmony_ci	{0x60e3, 0x0093}, {0x6005, 0x0093}, {0x6005, 0x0093}, {0x6000, 0x0093},
638c2ecf20Sopenharmony_ci	{0x6004, 0x0093}, {0x6000, 0x0093}, {0x6000, 0x0093}, {0x6000, 0x0093},
648c2ecf20Sopenharmony_ci	{0x6000, 0x0093}, {0x6000, 0x0093}, {0x6000, 0x0093}, {0x6000, 0x0093},
658c2ecf20Sopenharmony_ci	{0x6000, 0x0096}, {0x6008, 0x0097}, {0x6019, 0x0097}, {0x6002, 0x0097},
668c2ecf20Sopenharmony_ci	{0x600c, 0x0097}, {0x6024, 0x0097}, {0x6030, 0x0097}, {0x6028, 0x0097},
678c2ecf20Sopenharmony_ci	{0x6026, 0x0097}, {0x6002, 0x0097}, {0x6098, 0x0097}, {0x6080, 0x0097},
688c2ecf20Sopenharmony_ci	{0x6000, 0x0097}, {0x6000, 0x0097}, {0x60ed, 0x00c3}, {0x609a, 0x00c4},
698c2ecf20Sopenharmony_ci	{0x6000, 0x00a4}, {0x6011, 0x00c5}, {0x6051, 0x00c6}, {0x6010, 0x00c7},
708c2ecf20Sopenharmony_ci	{0x6066, 0x00b6}, {0x60a5, 0x00b8}, {0x6064, 0x00b7}, {0x607c, 0x00b9},
718c2ecf20Sopenharmony_ci	{0x60af, 0x00b3}, {0x6097, 0x00b4}, {0x60ff, 0x00b5}, {0x60c5, 0x00b0},
728c2ecf20Sopenharmony_ci	{0x6094, 0x00b1}, {0x600f, 0x00b2}, {0x605c, 0x00c4}, {0x6000, 0x00a8},
738c2ecf20Sopenharmony_ci	{0x60c8, 0x00c0}, {0x6096, 0x00c1}, {0x601d, 0x0086}, {0x6000, 0x0050},
748c2ecf20Sopenharmony_ci	{0x6090, 0x0051}, {0x6018, 0x0052}, {0x6000, 0x0053}, {0x6000, 0x0054},
758c2ecf20Sopenharmony_ci	{0x6088, 0x0055}, {0x6000, 0x0057}, {0x6090, 0x005a}, {0x6018, 0x005b},
768c2ecf20Sopenharmony_ci	{0x6005, 0x005c}, {0x60ed, 0x00c3}, {0x6000, 0x007f}, {0x6005, 0x00da},
778c2ecf20Sopenharmony_ci	{0x601f, 0x00e5}, {0x6067, 0x00e1}, {0x6000, 0x00e0}, {0x60ff, 0x00dd},
788c2ecf20Sopenharmony_ci	{0x6000, 0x0005}, {0x6001, 0x00ff}, {0x6000, 0x0000}, {0x6000, 0x0045},
798c2ecf20Sopenharmony_ci	{0x6000, 0x0010},
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic struct validx tbl_sensor_settings_common1[] = {
838c2ecf20Sopenharmony_ci	{0x0041, 0x0000}, {0x006a, 0x0007}, {0x00ef, 0x0006}, {0x006a, 0x000d},
848c2ecf20Sopenharmony_ci	{0x0000, 0x00c0}, {0x0010, 0x0010}, {0x0001, 0x00c1}, {0x0041, 0x00c2},
858c2ecf20Sopenharmony_ci	{0x0004, 0x00d8}, {0x0012, 0x0004}, {0x0000, 0x0058}, {0x0041, 0x0000},
868c2ecf20Sopenharmony_ci	{50, 0xffff},
878c2ecf20Sopenharmony_ci	{0x0061, 0x0000},
888c2ecf20Sopenharmony_ci	{0xffff, 0xffff},
898c2ecf20Sopenharmony_ci	{0x6000, 0x00ff}, {0x6000, 0x007c}, {0x6007, 0x007d},
908c2ecf20Sopenharmony_ci	{30, 0xffff},
918c2ecf20Sopenharmony_ci	{0x0040, 0x0000},
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic struct validx tbl_sensor_settings_common2[] = {
958c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6038, 0x000c},
968c2ecf20Sopenharmony_ci	{10, 0xffff},
978c2ecf20Sopenharmony_ci	{0x6000, 0x0011},
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic struct validx tbl_640[] = {
1018c2ecf20Sopenharmony_ci	{0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0}, {0x6067, 0x00e1},
1028c2ecf20Sopenharmony_ci	{0x6004, 0x00da}, {0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0},
1038c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6000, 0x0012}, {0x6000, 0x0011}, {0x6011, 0x0017},
1048c2ecf20Sopenharmony_ci	{0x6075, 0x0018}, {0x6001, 0x0019}, {0x6097, 0x001a}, {0x6036, 0x0032},
1058c2ecf20Sopenharmony_ci	{0x60bb, 0x004f}, {0x6057, 0x005a}, {0x609c, 0x0050}, {0x6080, 0x006d},
1068c2ecf20Sopenharmony_ci	{0x6092, 0x0026}, {0x60ff, 0x0020}, {0x6000, 0x0027}, {0x6000, 0x00ff},
1078c2ecf20Sopenharmony_ci	{0x60c8, 0x00c0}, {0x6096, 0x00c1}, {0x6000, 0x008c}, {0x603d, 0x0086},
1088c2ecf20Sopenharmony_ci	{0x6089, 0x0050}, {0x6090, 0x0051}, {0x602c, 0x0052}, {0x6000, 0x0053},
1098c2ecf20Sopenharmony_ci	{0x6000, 0x0054}, {0x6088, 0x0055}, {0x6000, 0x0057}, {0x60a0, 0x005a},
1108c2ecf20Sopenharmony_ci	{0x6078, 0x005b}, {0x6000, 0x005c}, {0x6004, 0x00d3}, {0x6000, 0x00e0},
1118c2ecf20Sopenharmony_ci	{0x60ff, 0x00dd}, {0x60a1, 0x005a},
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic struct validx tbl_800[] = {
1158c2ecf20Sopenharmony_ci	{0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0}, {0x6067, 0x00e1},
1168c2ecf20Sopenharmony_ci	{0x6004, 0x00da}, {0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0},
1178c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6040, 0x0012}, {0x6000, 0x0011}, {0x6011, 0x0017},
1188c2ecf20Sopenharmony_ci	{0x6043, 0x0018}, {0x6000, 0x0019}, {0x604b, 0x001a}, {0x6009, 0x0032},
1198c2ecf20Sopenharmony_ci	{0x60ca, 0x004f}, {0x60a8, 0x0050}, {0x6000, 0x006d}, {0x6038, 0x003d},
1208c2ecf20Sopenharmony_ci	{0x60c8, 0x0035}, {0x6000, 0x0022}, {0x6092, 0x0026}, {0x60ff, 0x0020},
1218c2ecf20Sopenharmony_ci	{0x6000, 0x0027}, {0x6000, 0x00ff}, {0x6064, 0x00c0}, {0x604b, 0x00c1},
1228c2ecf20Sopenharmony_ci	{0x6000, 0x008c}, {0x601d, 0x0086}, {0x6082, 0x00d3}, {0x6000, 0x00e0},
1238c2ecf20Sopenharmony_ci	{0x60ff, 0x00dd}, {0x6020, 0x008c}, {0x6001, 0x00ff}, {0x6044, 0x0018},
1248c2ecf20Sopenharmony_ci};
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic struct validx tbl_big1[] = {
1278c2ecf20Sopenharmony_ci	{0x0002, 0x00c1}, {0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0},
1288c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6000, 0x0012}, {0x6000, 0x0000}, {0x6000, 0x0045},
1298c2ecf20Sopenharmony_ci	{0x6000, 0x0010}, {0x6000, 0x0011}, {0x6011, 0x0017}, {0x6075, 0x0018},
1308c2ecf20Sopenharmony_ci	{0x6001, 0x0019}, {0x6097, 0x001a}, {0x6036, 0x0032}, {0x60bb, 0x004f},
1318c2ecf20Sopenharmony_ci	{0x609c, 0x0050}, {0x6057, 0x005a}, {0x6080, 0x006d}, {0x6043, 0x000f},
1328c2ecf20Sopenharmony_ci	{0x608f, 0x0003}, {0x6005, 0x007c}, {0x6081, 0x0026}, {0x6000, 0x00ff},
1338c2ecf20Sopenharmony_ci	{0x60c8, 0x00c0}, {0x6096, 0x00c1}, {0x6000, 0x008c},
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic struct validx tbl_big2[] = {
1378c2ecf20Sopenharmony_ci	{0x603d, 0x0086}, {0x6000, 0x0050}, {0x6090, 0x0051}, {0x602c, 0x0052},
1388c2ecf20Sopenharmony_ci	{0x6000, 0x0053}, {0x6000, 0x0054}, {0x6088, 0x0055}, {0x6000, 0x0057},
1398c2ecf20Sopenharmony_ci	{0x6040, 0x005a}, {0x60f0, 0x005b}, {0x6001, 0x005c}, {0x6082, 0x00d3},
1408c2ecf20Sopenharmony_ci	{0x6000, 0x008e},
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct validx tbl_big3[] = {
1448c2ecf20Sopenharmony_ci	{0x6004, 0x00da}, {0x6000, 0x00e0}, {0x6067, 0x00e1}, {0x60ff, 0x00dd},
1458c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6000, 0x00ff}, {0x60f1, 0x00dd}, {0x6004, 0x00e0},
1468c2ecf20Sopenharmony_ci	{0x6001, 0x00ff}, {0x6000, 0x0011}, {0x6000, 0x00ff}, {0x6010, 0x00c7},
1478c2ecf20Sopenharmony_ci	{0x6000, 0x0092}, {0x6006, 0x0093}, {0x60e3, 0x0093}, {0x6005, 0x0093},
1488c2ecf20Sopenharmony_ci	{0x6005, 0x0093}, {0x60ed, 0x00c3}, {0x6000, 0x00a4}, {0x60d0, 0x0087},
1498c2ecf20Sopenharmony_ci	{0x6003, 0x0096}, {0x600c, 0x0097}, {0x6024, 0x0097}, {0x6030, 0x0097},
1508c2ecf20Sopenharmony_ci	{0x6028, 0x0097}, {0x6026, 0x0097}, {0x6002, 0x0097}, {0x6001, 0x00ff},
1518c2ecf20Sopenharmony_ci	{0x6043, 0x000f}, {0x608f, 0x0003}, {0x6000, 0x002d}, {0x6000, 0x002e},
1528c2ecf20Sopenharmony_ci	{0x600a, 0x0022}, {0x6002, 0x0070}, {0x6008, 0x0014}, {0x6048, 0x0014},
1538c2ecf20Sopenharmony_ci	{0x6000, 0x00ff}, {0x6000, 0x00e0}, {0x60ff, 0x00dd},
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic struct validx tbl_post_unset_alt[] = {
1578c2ecf20Sopenharmony_ci	{0x006a, 0x000d}, {0x6001, 0x00ff}, {0x6081, 0x0026}, {0x6000, 0x0000},
1588c2ecf20Sopenharmony_ci	{0x6000, 0x0045}, {0x6000, 0x0010}, {0x6068, 0x000d},
1598c2ecf20Sopenharmony_ci	{50, 0xffff},
1608c2ecf20Sopenharmony_ci	{0x0021, 0x0000},
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic int  ov2640_init_at_startup(struct gspca_dev *gspca_dev);
1648c2ecf20Sopenharmony_cistatic int  ov2640_configure_alt(struct gspca_dev *gspca_dev);
1658c2ecf20Sopenharmony_cistatic int  ov2640_init_pre_alt(struct gspca_dev *gspca_dev);
1668c2ecf20Sopenharmony_cistatic int  ov2640_init_post_alt(struct gspca_dev *gspca_dev);
1678c2ecf20Sopenharmony_cistatic void ov2640_post_unset_alt(struct gspca_dev *gspca_dev);
1688c2ecf20Sopenharmony_cistatic int  ov2640_camera_settings(struct gspca_dev *gspca_dev);
1698c2ecf20Sopenharmony_ci/*==========================================================================*/
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_civoid ov2640_init_settings(struct gspca_dev *gspca_dev)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	sd->vcur.backlight  =  32;
1768c2ecf20Sopenharmony_ci	sd->vcur.brightness =   0;
1778c2ecf20Sopenharmony_ci	sd->vcur.sharpness  =   6;
1788c2ecf20Sopenharmony_ci	sd->vcur.contrast   =   0;
1798c2ecf20Sopenharmony_ci	sd->vcur.gamma      =  32;
1808c2ecf20Sopenharmony_ci	sd->vcur.hue        =   0;
1818c2ecf20Sopenharmony_ci	sd->vcur.saturation = 128;
1828c2ecf20Sopenharmony_ci	sd->vcur.whitebal   =  64;
1838c2ecf20Sopenharmony_ci	sd->vcur.mirror     =   0;
1848c2ecf20Sopenharmony_ci	sd->vcur.flip       =   0;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	sd->vmax.backlight  =  64;
1878c2ecf20Sopenharmony_ci	sd->vmax.brightness = 255;
1888c2ecf20Sopenharmony_ci	sd->vmax.sharpness  =  31;
1898c2ecf20Sopenharmony_ci	sd->vmax.contrast   = 255;
1908c2ecf20Sopenharmony_ci	sd->vmax.gamma      =  64;
1918c2ecf20Sopenharmony_ci	sd->vmax.hue        = 254 + 2;
1928c2ecf20Sopenharmony_ci	sd->vmax.saturation = 255;
1938c2ecf20Sopenharmony_ci	sd->vmax.whitebal   = 128;
1948c2ecf20Sopenharmony_ci	sd->vmax.mirror     = 1;
1958c2ecf20Sopenharmony_ci	sd->vmax.flip       = 1;
1968c2ecf20Sopenharmony_ci	sd->vmax.AC50Hz     = 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	sd->dev_camera_settings = ov2640_camera_settings;
1998c2ecf20Sopenharmony_ci	sd->dev_init_at_startup = ov2640_init_at_startup;
2008c2ecf20Sopenharmony_ci	sd->dev_configure_alt   = ov2640_configure_alt;
2018c2ecf20Sopenharmony_ci	sd->dev_init_pre_alt    = ov2640_init_pre_alt;
2028c2ecf20Sopenharmony_ci	sd->dev_post_unset_alt  = ov2640_post_unset_alt;
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/*==========================================================================*/
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic void common(struct gspca_dev *gspca_dev)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	fetch_validx(gspca_dev, tbl_common, ARRAY_SIZE(tbl_common));
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int ov2640_init_at_startup(struct gspca_dev *gspca_dev)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	fetch_validx(gspca_dev, tbl_init_at_startup,
2158c2ecf20Sopenharmony_ci			ARRAY_SIZE(tbl_init_at_startup));
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, dat_init1);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	common(gspca_dev);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0006, 1, c61);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 1, 0x00ef, 0x0006, 0, NULL);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, c51);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 1, 0x0051, 0x0000, 0, NULL);
2288c2ecf20Sopenharmony_ci/*	ctrl_out(gspca_dev, 0x40, 11, 0x0000, 0x0000, 0, NULL); */
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	return 0;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic int ov2640_init_pre_alt(struct gspca_dev *gspca_dev)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	sd->mirrorMask = 0;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	sd->vold.backlight  = -1;
2408c2ecf20Sopenharmony_ci	sd->vold.brightness = -1;
2418c2ecf20Sopenharmony_ci	sd->vold.sharpness  = -1;
2428c2ecf20Sopenharmony_ci	sd->vold.contrast   = -1;
2438c2ecf20Sopenharmony_ci	sd->vold.saturation = -1;
2448c2ecf20Sopenharmony_ci	sd->vold.gamma    = -1;
2458c2ecf20Sopenharmony_ci	sd->vold.hue      = -1;
2468c2ecf20Sopenharmony_ci	sd->vold.whitebal = -1;
2478c2ecf20Sopenharmony_ci	sd->vold.mirror = -1;
2488c2ecf20Sopenharmony_ci	sd->vold.flip   = -1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	ov2640_init_post_alt(gspca_dev);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return 0;
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic int ov2640_init_post_alt(struct gspca_dev *gspca_dev)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
2588c2ecf20Sopenharmony_ci	s32 n; /* reserved for FETCH functions */
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 5, 0x0001, 0x0000, 0, NULL);
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	n = fetch_validx(gspca_dev, tbl_sensor_settings_common1,
2638c2ecf20Sopenharmony_ci			ARRAY_SIZE(tbl_sensor_settings_common1));
2648c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, dat_post);
2658c2ecf20Sopenharmony_ci	common(gspca_dev);
2668c2ecf20Sopenharmony_ci	keep_on_fetching_validx(gspca_dev, tbl_sensor_settings_common1,
2678c2ecf20Sopenharmony_ci				ARRAY_SIZE(tbl_sensor_settings_common1), n);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	switch (reso) {
2708c2ecf20Sopenharmony_ci	case IMAGE_640:
2718c2ecf20Sopenharmony_ci		n = fetch_validx(gspca_dev, tbl_640, ARRAY_SIZE(tbl_640));
2728c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, dat_640);
2738c2ecf20Sopenharmony_ci		break;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	case IMAGE_800:
2768c2ecf20Sopenharmony_ci		n = fetch_validx(gspca_dev, tbl_800, ARRAY_SIZE(tbl_800));
2778c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200, 12, dat_800);
2788c2ecf20Sopenharmony_ci		break;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	case IMAGE_1600:
2818c2ecf20Sopenharmony_ci	case IMAGE_1280:
2828c2ecf20Sopenharmony_ci		n = fetch_validx(gspca_dev, tbl_big1, ARRAY_SIZE(tbl_big1));
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci		if (reso == IMAGE_1280) {
2858c2ecf20Sopenharmony_ci			n = fetch_validx(gspca_dev, tbl_big2,
2868c2ecf20Sopenharmony_ci					ARRAY_SIZE(tbl_big2));
2878c2ecf20Sopenharmony_ci		} else {
2888c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x601d, 0x0086, 0, NULL);
2898c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6001, 0x00d7, 0, NULL);
2908c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6082, 0x00d3, 0, NULL);
2918c2ecf20Sopenharmony_ci		}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci		n = fetch_validx(gspca_dev, tbl_big3, ARRAY_SIZE(tbl_big3));
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		if (reso == IMAGE_1280) {
2968c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6001, 0x00ff, 0, NULL);
2978c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200,
2988c2ecf20Sopenharmony_ci					12, dat_1280);
2998c2ecf20Sopenharmony_ci		} else {
3008c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6020, 0x008c, 0, NULL);
3018c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6001, 0x00ff, 0, NULL);
3028c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 1, 0x6076, 0x0018, 0, NULL);
3038c2ecf20Sopenharmony_ci			ctrl_out(gspca_dev, 0x40, 3, 0x0000, 0x0200,
3048c2ecf20Sopenharmony_ci					12, dat_1600);
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci		break;
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	n = fetch_validx(gspca_dev, tbl_sensor_settings_common2,
3108c2ecf20Sopenharmony_ci			ARRAY_SIZE(tbl_sensor_settings_common2));
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	ov2640_camera_settings(gspca_dev);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	return 0;
3158c2ecf20Sopenharmony_ci}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic int ov2640_configure_alt(struct gspca_dev *gspca_dev)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	s32 reso = gspca_dev->cam.cam_mode[(s32) gspca_dev->curr_mode].priv;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	switch (reso) {
3228c2ecf20Sopenharmony_ci	case IMAGE_640:
3238c2ecf20Sopenharmony_ci		gspca_dev->alt = 3 + 1;
3248c2ecf20Sopenharmony_ci		break;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	case IMAGE_800:
3278c2ecf20Sopenharmony_ci	case IMAGE_1280:
3288c2ecf20Sopenharmony_ci	case IMAGE_1600:
3298c2ecf20Sopenharmony_ci		gspca_dev->alt = 1 + 1;
3308c2ecf20Sopenharmony_ci		break;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic int ov2640_camera_settings(struct gspca_dev *gspca_dev)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	struct sd *sd = (struct sd *) gspca_dev;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	s32 backlight = sd->vcur.backlight;
3408c2ecf20Sopenharmony_ci	s32 bright = sd->vcur.brightness;
3418c2ecf20Sopenharmony_ci	s32 sharp  = sd->vcur.sharpness;
3428c2ecf20Sopenharmony_ci	s32 gam    = sd->vcur.gamma;
3438c2ecf20Sopenharmony_ci	s32 cntr   = sd->vcur.contrast;
3448c2ecf20Sopenharmony_ci	s32 sat    = sd->vcur.saturation;
3458c2ecf20Sopenharmony_ci	s32 hue    = sd->vcur.hue;
3468c2ecf20Sopenharmony_ci	s32 wbal   = sd->vcur.whitebal;
3478c2ecf20Sopenharmony_ci	s32 mirror = (((sd->vcur.mirror > 0) ^ sd->mirrorMask) == 0);
3488c2ecf20Sopenharmony_ci	s32 flip   = (((sd->vcur.flip   > 0) ^ sd->mirrorMask) == 0);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (backlight != sd->vold.backlight) {
3518c2ecf20Sopenharmony_ci		/* No sd->vold.backlight=backlight; (to be done again later) */
3528c2ecf20Sopenharmony_ci		if (backlight < 0 || backlight > sd->vmax.backlight)
3538c2ecf20Sopenharmony_ci			backlight = 0;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001                 , 0x00ff,
3568c2ecf20Sopenharmony_ci				0, NULL);
3578c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x601e + backlight     , 0x0024,
3588c2ecf20Sopenharmony_ci				0, NULL);
3598c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x601e + backlight - 10, 0x0025,
3608c2ecf20Sopenharmony_ci				0, NULL);
3618c2ecf20Sopenharmony_ci	}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	if (bright != sd->vold.brightness) {
3648c2ecf20Sopenharmony_ci		sd->vold.brightness = bright;
3658c2ecf20Sopenharmony_ci		if (bright < 0 || bright > sd->vmax.brightness)
3668c2ecf20Sopenharmony_ci			bright = 0;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000         , 0x00ff, 0, NULL);
3698c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6009         , 0x007c, 0, NULL);
3708c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + bright, 0x007d, 0, NULL);
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (wbal != sd->vold.whitebal) {
3748c2ecf20Sopenharmony_ci		sd->vold.whitebal = wbal;
3758c2ecf20Sopenharmony_ci		if (wbal < 0 || wbal > sd->vmax.whitebal)
3768c2ecf20Sopenharmony_ci			wbal = 0;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000       , 0x00ff, 0, NULL);
3798c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6003       , 0x007c, 0, NULL);
3808c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + wbal, 0x007d, 0, NULL);
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (cntr != sd->vold.contrast) {
3848c2ecf20Sopenharmony_ci		sd->vold.contrast = cntr;
3858c2ecf20Sopenharmony_ci		if (cntr < 0 || cntr > sd->vmax.contrast)
3868c2ecf20Sopenharmony_ci			cntr = 0;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000       , 0x00ff, 0, NULL);
3898c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6007       , 0x007c, 0, NULL);
3908c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + cntr, 0x007d, 0, NULL);
3918c2ecf20Sopenharmony_ci	}
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	if (sat != sd->vold.saturation) {
3948c2ecf20Sopenharmony_ci		sd->vold.saturation = sat;
3958c2ecf20Sopenharmony_ci		if (sat < 0 || sat > sd->vmax.saturation)
3968c2ecf20Sopenharmony_ci			sat = 0;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000      , 0x00ff, 0, NULL);
3998c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001      , 0x007c, 0, NULL);
4008c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + sat, 0x007d, 0, NULL);
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if (sharp != sd->vold.sharpness) {
4048c2ecf20Sopenharmony_ci		sd->vold.sharpness = sharp;
4058c2ecf20Sopenharmony_ci		if (sharp < 0 || sharp > sd->vmax.sharpness)
4068c2ecf20Sopenharmony_ci			sharp = 0;
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000        , 0x00ff, 0, NULL);
4098c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001        , 0x0092, 0, NULL);
4108c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x60c0 + sharp, 0x0093, 0, NULL);
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (hue != sd->vold.hue) {
4148c2ecf20Sopenharmony_ci		sd->vold.hue = hue;
4158c2ecf20Sopenharmony_ci		if (hue < 0 || hue > sd->vmax.hue)
4168c2ecf20Sopenharmony_ci			hue = 0;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000     , 0x00ff, 0, NULL);
4198c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6002     , 0x007c, 0, NULL);
4208c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + hue * (hue < 255), 0x007d,
4218c2ecf20Sopenharmony_ci				0, NULL);
4228c2ecf20Sopenharmony_ci		if (hue >= 255)
4238c2ecf20Sopenharmony_ci			sd->swapRB = 1;
4248c2ecf20Sopenharmony_ci		else
4258c2ecf20Sopenharmony_ci			sd->swapRB = 0;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (gam != sd->vold.gamma) {
4298c2ecf20Sopenharmony_ci		sd->vold.gamma = gam;
4308c2ecf20Sopenharmony_ci		if (gam < 0 || gam > sd->vmax.gamma)
4318c2ecf20Sopenharmony_ci			gam = 0;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000      , 0x00ff, 0, NULL);
4348c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6008      , 0x007c, 0, NULL);
4358c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000 + gam, 0x007d, 0, NULL);
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (mirror != sd->vold.mirror || flip != sd->vold.flip) {
4398c2ecf20Sopenharmony_ci		sd->vold.mirror = mirror;
4408c2ecf20Sopenharmony_ci		sd->vold.flip   = flip;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		mirror = 0x80 * mirror;
4438c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001, 0x00ff, 0, NULL);
4448c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x8004, 0, NULL);
4458c2ecf20Sopenharmony_ci		ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x8004, 1, c28);
4468c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6028 + mirror, 0x0004, 0, NULL);
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci		flip = 0x50 * flip + mirror;
4498c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001, 0x00ff, 0, NULL);
4508c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x8004, 0, NULL);
4518c2ecf20Sopenharmony_ci		ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x8004, 1, ca8);
4528c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6028 + flip, 0x0004, 0, NULL);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci		ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, c50);
4558c2ecf20Sopenharmony_ci	}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (backlight != sd->vold.backlight) {
4588c2ecf20Sopenharmony_ci		sd->vold.backlight = backlight;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x6001                 , 0x00ff,
4618c2ecf20Sopenharmony_ci				0, NULL);
4628c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x601e + backlight     , 0x0024,
4638c2ecf20Sopenharmony_ci				0, NULL);
4648c2ecf20Sopenharmony_ci		ctrl_out(gspca_dev, 0x40, 1, 0x601e + backlight - 10, 0x0025,
4658c2ecf20Sopenharmony_ci				0, NULL);
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	return 0;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic void ov2640_post_unset_alt(struct gspca_dev *gspca_dev)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	ctrl_out(gspca_dev, 0x40, 5, 0x0000, 0x0000, 0, NULL);
4748c2ecf20Sopenharmony_ci	msleep(20);
4758c2ecf20Sopenharmony_ci	fetch_validx(gspca_dev, tbl_post_unset_alt,
4768c2ecf20Sopenharmony_ci			ARRAY_SIZE(tbl_post_unset_alt));
4778c2ecf20Sopenharmony_ci}
478