m5602_ov9650.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Driver for the ov9650 sensor
  3. *
  4. * Copyright (C) 2008 Erik Andrén
  5. * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #include "m5602_ov9650.h"
  19. /* Vertically and horizontally flips the image if matched, needed for machines
  20. where the sensor is mounted upside down */
  21. static
  22. const
  23. struct dmi_system_id ov9650_flip_dmi_table[] = {
  24. {
  25. .ident = "ASUS A6VC",
  26. .matches = {
  27. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  28. DMI_MATCH(DMI_PRODUCT_NAME, "A6VC")
  29. }
  30. },
  31. {
  32. .ident = "ASUS A6VM",
  33. .matches = {
  34. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  35. DMI_MATCH(DMI_PRODUCT_NAME, "A6VM")
  36. }
  37. },
  38. {
  39. .ident = "ASUS A6JC",
  40. .matches = {
  41. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  42. DMI_MATCH(DMI_PRODUCT_NAME, "A6JC")
  43. }
  44. },
  45. {
  46. .ident = "ASUS A6Ja",
  47. .matches = {
  48. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  49. DMI_MATCH(DMI_PRODUCT_NAME, "A6J")
  50. }
  51. },
  52. {
  53. .ident = "ASUS A6Kt",
  54. .matches = {
  55. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
  56. DMI_MATCH(DMI_PRODUCT_NAME, "A6Kt")
  57. }
  58. },
  59. {
  60. .ident = "Alienware Aurora m9700",
  61. .matches = {
  62. DMI_MATCH(DMI_SYS_VENDOR, "alienware"),
  63. DMI_MATCH(DMI_PRODUCT_NAME, "Aurora m9700")
  64. }
  65. },
  66. { }
  67. };
  68. static void ov9650_dump_registers(struct sd *sd);
  69. int ov9650_probe(struct sd *sd)
  70. {
  71. u8 prod_id = 0, ver_id = 0, i;
  72. if (force_sensor) {
  73. if (force_sensor == OV9650_SENSOR) {
  74. info("Forcing an %s sensor", ov9650.name);
  75. goto sensor_found;
  76. }
  77. /* If we want to force another sensor,
  78. don't try to probe this one */
  79. return -ENODEV;
  80. }
  81. info("Probing for an ov9650 sensor");
  82. /* Run the pre-init to actually probe the unit */
  83. for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
  84. u8 data = preinit_ov9650[i][2];
  85. if (preinit_ov9650[i][0] == SENSOR)
  86. m5602_write_sensor(sd,
  87. preinit_ov9650[i][1], &data, 1);
  88. else
  89. m5602_write_bridge(sd, preinit_ov9650[i][1], data);
  90. }
  91. if (m5602_read_sensor(sd, OV9650_PID, &prod_id, 1))
  92. return -ENODEV;
  93. if (m5602_read_sensor(sd, OV9650_VER, &ver_id, 1))
  94. return -ENODEV;
  95. if ((prod_id == 0x96) && (ver_id == 0x52)) {
  96. info("Detected an ov9650 sensor");
  97. goto sensor_found;
  98. }
  99. return -ENODEV;
  100. sensor_found:
  101. sd->gspca_dev.cam.cam_mode = ov9650.modes;
  102. sd->gspca_dev.cam.nmodes = ov9650.nmodes;
  103. sd->desc->ctrls = ov9650.ctrls;
  104. sd->desc->nctrls = ARRAY_SIZE(ov9650_ctrls);
  105. return 0;
  106. }
  107. int ov9650_init(struct sd *sd)
  108. {
  109. int i, err = 0;
  110. u8 data;
  111. if (dump_sensor)
  112. ov9650_dump_registers(sd);
  113. for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
  114. data = init_ov9650[i][2];
  115. if (init_ov9650[i][0] == SENSOR)
  116. err = m5602_write_sensor(sd, init_ov9650[i][1],
  117. &data, 1);
  118. else
  119. err = m5602_write_bridge(sd, init_ov9650[i][1], data);
  120. }
  121. if (dmi_check_system(ov9650_flip_dmi_table) && !err) {
  122. info("vflip quirk active");
  123. data = 0x30;
  124. err = m5602_write_sensor(sd, OV9650_MVFP, &data, 1);
  125. }
  126. return err;
  127. }
  128. int ov9650_start(struct sd *sd)
  129. {
  130. int i, err = 0;
  131. struct cam *cam = &sd->gspca_dev.cam;
  132. err = ov9650_init(sd);
  133. if (err < 0)
  134. return err;
  135. for (i = 0; i < ARRAY_SIZE(res_init_ov9650) && !err; i++) {
  136. if (res_init_ov9650[i][0] == BRIDGE)
  137. err = m5602_write_bridge(sd, res_init_ov9650[i][1],
  138. res_init_ov9650[i][2]);
  139. else if (res_init_ov9650[i][0] == SENSOR) {
  140. u8 data = res_init_ov9650[i][2];
  141. err = m5602_write_sensor(sd,
  142. res_init_ov9650[i][1], &data, 1);
  143. }
  144. }
  145. if (err < 0)
  146. return err;
  147. switch (cam->cam_mode[sd->gspca_dev.curr_mode].width) {
  148. case 640:
  149. PDEBUG(D_V4L2, "Configuring camera for VGA mode");
  150. for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
  151. if (VGA_ov9650[i][0] == SENSOR) {
  152. u8 data = VGA_ov9650[i][2];
  153. err = m5602_write_sensor(sd,
  154. VGA_ov9650[i][1], &data, 1);
  155. } else {
  156. err = m5602_write_bridge(sd, VGA_ov9650[i][1],
  157. VGA_ov9650[i][2]);
  158. }
  159. }
  160. break;
  161. case 352:
  162. PDEBUG(D_V4L2, "Configuring camera for CIF mode");
  163. for (i = 0; i < ARRAY_SIZE(CIF_ov9650) && !err; i++) {
  164. if (CIF_ov9650[i][0] == SENSOR) {
  165. u8 data = CIF_ov9650[i][2];
  166. err = m5602_write_sensor(sd,
  167. CIF_ov9650[i][1], &data, 1);
  168. } else {
  169. err = m5602_write_bridge(sd, CIF_ov9650[i][1],
  170. CIF_ov9650[i][2]);
  171. }
  172. }
  173. break;
  174. case 320:
  175. PDEBUG(D_V4L2, "Configuring camera for QVGA mode");
  176. for (i = 0; i < ARRAY_SIZE(QVGA_ov9650) && !err; i++) {
  177. if (QVGA_ov9650[i][0] == SENSOR) {
  178. u8 data = QVGA_ov9650[i][2];
  179. err = m5602_write_sensor(sd,
  180. QVGA_ov9650[i][1], &data, 1);
  181. } else {
  182. err = m5602_write_bridge(sd, QVGA_ov9650[i][1],
  183. QVGA_ov9650[i][2]);
  184. }
  185. }
  186. break;
  187. case 176:
  188. PDEBUG(D_V4L2, "Configuring camera for QCIF mode");
  189. for (i = 0; i < ARRAY_SIZE(QCIF_ov9650) && !err; i++) {
  190. if (QCIF_ov9650[i][0] == SENSOR) {
  191. u8 data = QCIF_ov9650[i][2];
  192. err = m5602_write_sensor(sd,
  193. QCIF_ov9650[i][1], &data, 1);
  194. } else {
  195. err = m5602_write_bridge(sd, QCIF_ov9650[i][1],
  196. QCIF_ov9650[i][2]);
  197. }
  198. }
  199. break;
  200. }
  201. return err;
  202. }
  203. int ov9650_stop(struct sd *sd)
  204. {
  205. u8 data = OV9650_SOFT_SLEEP | OV9650_OUTPUT_DRIVE_2X;
  206. return m5602_write_sensor(sd, OV9650_COM2, &data, 1);
  207. }
  208. int ov9650_power_down(struct sd *sd)
  209. {
  210. int i, err = 0;
  211. for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
  212. u8 data = power_down_ov9650[i][2];
  213. if (power_down_ov9650[i][0] == SENSOR)
  214. err = m5602_write_sensor(sd,
  215. power_down_ov9650[i][1], &data, 1);
  216. else
  217. err = m5602_write_bridge(sd, power_down_ov9650[i][1],
  218. data);
  219. }
  220. return err;
  221. }
  222. int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  223. {
  224. struct sd *sd = (struct sd *) gspca_dev;
  225. u8 i2c_data;
  226. int err;
  227. err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
  228. if (err < 0)
  229. return err;
  230. *val = i2c_data & 0x03;
  231. err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
  232. if (err < 0)
  233. return err;
  234. *val |= (i2c_data << 2);
  235. err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
  236. if (err < 0)
  237. return err;
  238. *val |= (i2c_data & 0x3f) << 10;
  239. PDEBUG(D_V4L2, "Read exposure %d", *val);
  240. return err;
  241. }
  242. int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  243. {
  244. struct sd *sd = (struct sd *) gspca_dev;
  245. u8 i2c_data;
  246. int err;
  247. PDEBUG(D_V4L2, "Set exposure to %d",
  248. val & 0xffff);
  249. /* The 6 MSBs */
  250. i2c_data = (val >> 10) & 0x3f;
  251. err = m5602_write_sensor(sd, OV9650_AECHM,
  252. &i2c_data, 1);
  253. if (err < 0)
  254. return err;
  255. /* The 8 middle bits */
  256. i2c_data = (val >> 2) & 0xff;
  257. err = m5602_write_sensor(sd, OV9650_AECH,
  258. &i2c_data, 1);
  259. if (err < 0)
  260. return err;
  261. /* The 2 LSBs */
  262. i2c_data = val & 0x03;
  263. err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
  264. return err;
  265. }
  266. int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  267. {
  268. int err;
  269. u8 i2c_data;
  270. struct sd *sd = (struct sd *) gspca_dev;
  271. m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  272. *val = (i2c_data & 0x03) << 8;
  273. err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  274. *val |= i2c_data;
  275. PDEBUG(D_V4L2, "Read gain %d", *val);
  276. return err;
  277. }
  278. int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  279. {
  280. int err;
  281. u8 i2c_data;
  282. struct sd *sd = (struct sd *) gspca_dev;
  283. /* The 2 MSB */
  284. /* Read the OV9650_VREF register first to avoid
  285. corrupting the VREF high and low bits */
  286. m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  287. /* Mask away all uninteresting bits */
  288. i2c_data = ((val & 0x0300) >> 2) |
  289. (i2c_data & 0x3F);
  290. err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  291. /* The 8 LSBs */
  292. i2c_data = val & 0xff;
  293. err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  294. return err;
  295. }
  296. int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  297. {
  298. int err;
  299. u8 i2c_data;
  300. struct sd *sd = (struct sd *) gspca_dev;
  301. err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
  302. *val = i2c_data;
  303. PDEBUG(D_V4L2, "Read red gain %d", *val);
  304. return err;
  305. }
  306. int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  307. {
  308. int err;
  309. u8 i2c_data;
  310. struct sd *sd = (struct sd *) gspca_dev;
  311. PDEBUG(D_V4L2, "Set red gain to %d",
  312. val & 0xff);
  313. i2c_data = val & 0xff;
  314. err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
  315. return err;
  316. }
  317. int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  318. {
  319. int err;
  320. u8 i2c_data;
  321. struct sd *sd = (struct sd *) gspca_dev;
  322. err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  323. *val = i2c_data;
  324. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  325. return err;
  326. }
  327. int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  328. {
  329. int err;
  330. u8 i2c_data;
  331. struct sd *sd = (struct sd *) gspca_dev;
  332. PDEBUG(D_V4L2, "Set blue gain to %d",
  333. val & 0xff);
  334. i2c_data = val & 0xff;
  335. err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  336. return err;
  337. }
  338. int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  339. {
  340. int err;
  341. u8 i2c_data;
  342. struct sd *sd = (struct sd *) gspca_dev;
  343. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  344. if (dmi_check_system(ov9650_flip_dmi_table))
  345. *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
  346. else
  347. *val = (i2c_data & OV9650_HFLIP) >> 5;
  348. PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
  349. return err;
  350. }
  351. int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  352. {
  353. int err;
  354. u8 i2c_data;
  355. struct sd *sd = (struct sd *) gspca_dev;
  356. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  357. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  358. if (err < 0)
  359. return err;
  360. if (dmi_check_system(ov9650_flip_dmi_table))
  361. i2c_data = ((i2c_data & 0xdf) |
  362. (((val ? 0 : 1) & 0x01) << 5));
  363. else
  364. i2c_data = ((i2c_data & 0xdf) |
  365. ((val & 0x01) << 5));
  366. err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  367. return err;
  368. }
  369. int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  370. {
  371. int err;
  372. u8 i2c_data;
  373. struct sd *sd = (struct sd *) gspca_dev;
  374. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  375. if (dmi_check_system(ov9650_flip_dmi_table))
  376. *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
  377. else
  378. *val = (i2c_data & 0x10) >> 4;
  379. PDEBUG(D_V4L2, "Read vertical flip %d", *val);
  380. return err;
  381. }
  382. int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  383. {
  384. int err;
  385. u8 i2c_data;
  386. struct sd *sd = (struct sd *) gspca_dev;
  387. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  388. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  389. if (err < 0)
  390. return err;
  391. if (dmi_check_system(ov9650_flip_dmi_table))
  392. i2c_data = ((i2c_data & 0xef) |
  393. (((val ? 0 : 1) & 0x01) << 4));
  394. else
  395. i2c_data = ((i2c_data & 0xef) |
  396. ((val & 0x01) << 4));
  397. err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  398. return err;
  399. }
  400. int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
  401. {
  402. int err;
  403. u8 i2c_data;
  404. struct sd *sd = (struct sd *) gspca_dev;
  405. err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  406. if (err < 0)
  407. return err;
  408. *val = (i2c_data & 0x03) << 8;
  409. err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  410. *val |= i2c_data;
  411. PDEBUG(D_V4L2, "Read gain %d", *val);
  412. return err;
  413. }
  414. int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  415. {
  416. int err;
  417. u8 i2c_data;
  418. struct sd *sd = (struct sd *) gspca_dev;
  419. PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
  420. /* Read the OV9650_VREF register first to avoid
  421. corrupting the VREF high and low bits */
  422. err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  423. if (err < 0)
  424. return err;
  425. /* Mask away all uninteresting bits */
  426. i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
  427. err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  428. if (err < 0)
  429. return err;
  430. /* The 8 LSBs */
  431. i2c_data = val & 0xff;
  432. err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  433. return err;
  434. }
  435. int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
  436. {
  437. int err;
  438. u8 i2c_data;
  439. struct sd *sd = (struct sd *) gspca_dev;
  440. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  441. *val = (i2c_data & OV9650_AWB_EN) >> 1;
  442. PDEBUG(D_V4L2, "Read auto white balance %d", *val);
  443. return err;
  444. }
  445. int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
  446. {
  447. int err;
  448. u8 i2c_data;
  449. struct sd *sd = (struct sd *) gspca_dev;
  450. PDEBUG(D_V4L2, "Set auto white balance to %d", val);
  451. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  452. if (err < 0)
  453. return err;
  454. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  455. err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  456. return err;
  457. }
  458. int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
  459. {
  460. int err;
  461. u8 i2c_data;
  462. struct sd *sd = (struct sd *) gspca_dev;
  463. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  464. *val = (i2c_data & OV9650_AGC_EN) >> 2;
  465. PDEBUG(D_V4L2, "Read auto gain control %d", *val);
  466. return err;
  467. }
  468. int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  469. {
  470. int err;
  471. u8 i2c_data;
  472. struct sd *sd = (struct sd *) gspca_dev;
  473. PDEBUG(D_V4L2, "Set auto gain control to %d", val);
  474. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  475. if (err < 0)
  476. return err;
  477. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  478. err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  479. return err;
  480. }
  481. static void ov9650_dump_registers(struct sd *sd)
  482. {
  483. int address;
  484. info("Dumping the ov9650 register state");
  485. for (address = 0; address < 0xa9; address++) {
  486. u8 value;
  487. m5602_read_sensor(sd, address, &value, 1);
  488. info("register 0x%x contains 0x%x",
  489. address, value);
  490. }
  491. info("ov9650 register state dump complete");
  492. info("Probing for which registers that are read/write");
  493. for (address = 0; address < 0xff; address++) {
  494. u8 old_value, ctrl_value;
  495. u8 test_value[2] = {0xff, 0xff};
  496. m5602_read_sensor(sd, address, &old_value, 1);
  497. m5602_write_sensor(sd, address, test_value, 1);
  498. m5602_read_sensor(sd, address, &ctrl_value, 1);
  499. if (ctrl_value == test_value[0])
  500. info("register 0x%x is writeable", address);
  501. else
  502. info("register 0x%x is read only", address);
  503. /* Restore original value */
  504. m5602_write_sensor(sd, address, &old_value, 1);
  505. }
  506. }