m5602_ov9650.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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], res_init_ov9650[i][2]);
  138. else if (res_init_ov9650[i][0] == SENSOR) {
  139. u8 data = res_init_ov9650[i][2];
  140. err = m5602_write_sensor(sd, res_init_ov9650[i][1], &data, 1);
  141. }
  142. }
  143. if (err < 0)
  144. return err;
  145. switch (cam->cam_mode[sd->gspca_dev.curr_mode].width)
  146. {
  147. case 640:
  148. PDEBUG(D_V4L2, "Configuring camera for VGA mode");
  149. for (i = 0; i < ARRAY_SIZE(VGA_ov9650) && !err; i++) {
  150. if (VGA_ov9650[i][0] == SENSOR) {
  151. u8 data = VGA_ov9650[i][2];
  152. err = m5602_write_sensor(sd,
  153. VGA_ov9650[i][1], &data, 1);
  154. } else {
  155. err = m5602_write_bridge(sd, VGA_ov9650[i][1], VGA_ov9650[i][2]);
  156. }
  157. }
  158. break;
  159. case 352:
  160. PDEBUG(D_V4L2, "Configuring camera for CIF mode");
  161. for (i = 0; i < ARRAY_SIZE(CIF_ov9650) && !err; i++) {
  162. if (CIF_ov9650[i][0] == SENSOR) {
  163. u8 data = CIF_ov9650[i][2];
  164. err = m5602_write_sensor(sd,
  165. CIF_ov9650[i][1], &data, 1);
  166. } else {
  167. err = m5602_write_bridge(sd, CIF_ov9650[i][1], CIF_ov9650[i][2]);
  168. }
  169. }
  170. break;
  171. case 320:
  172. PDEBUG(D_V4L2, "Configuring camera for QVGA mode");
  173. for (i = 0; i < ARRAY_SIZE(QVGA_ov9650) && !err; i++) {
  174. if (QVGA_ov9650[i][0] == SENSOR) {
  175. u8 data = QVGA_ov9650[i][2];
  176. err = m5602_write_sensor(sd,
  177. QVGA_ov9650[i][1], &data, 1);
  178. } else {
  179. err = m5602_write_bridge(sd, QVGA_ov9650[i][1], QVGA_ov9650[i][2]);
  180. }
  181. }
  182. break;
  183. case 176:
  184. PDEBUG(D_V4L2, "Configuring camera for QCIF mode");
  185. for (i = 0; i < ARRAY_SIZE(QCIF_ov9650) && !err; i++) {
  186. if (QCIF_ov9650[i][0] == SENSOR) {
  187. u8 data = QCIF_ov9650[i][2];
  188. err = m5602_write_sensor(sd,
  189. QCIF_ov9650[i][1], &data, 1);
  190. } else {
  191. err = m5602_write_bridge(sd, QCIF_ov9650[i][1], QCIF_ov9650[i][2]);
  192. }
  193. }
  194. break;
  195. }
  196. return err;
  197. }
  198. int ov9650_stop(struct sd *sd)
  199. {
  200. u8 data = OV9650_SOFT_SLEEP | OV9650_OUTPUT_DRIVE_2X;
  201. return m5602_write_sensor(sd, OV9650_COM2, &data, 1);
  202. }
  203. int ov9650_power_down(struct sd *sd)
  204. {
  205. int i, err = 0;
  206. for (i = 0; i < ARRAY_SIZE(power_down_ov9650) && !err; i++) {
  207. u8 data = power_down_ov9650[i][2];
  208. if (power_down_ov9650[i][0] == SENSOR)
  209. err = m5602_write_sensor(sd,
  210. power_down_ov9650[i][1], &data, 1);
  211. else
  212. err = m5602_write_bridge(sd, power_down_ov9650[i][1],
  213. data);
  214. }
  215. return err;
  216. }
  217. int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  218. {
  219. struct sd *sd = (struct sd *) gspca_dev;
  220. u8 i2c_data;
  221. int err;
  222. err = m5602_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
  223. if (err < 0)
  224. return err;
  225. *val = i2c_data & 0x03;
  226. err = m5602_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
  227. if (err < 0)
  228. return err;
  229. *val |= (i2c_data << 2);
  230. err = m5602_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
  231. if (err < 0)
  232. return err;
  233. *val |= (i2c_data & 0x3f) << 10;
  234. PDEBUG(D_V4L2, "Read exposure %d", *val);
  235. return err;
  236. }
  237. int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  238. {
  239. struct sd *sd = (struct sd *) gspca_dev;
  240. u8 i2c_data;
  241. int err;
  242. PDEBUG(D_V4L2, "Set exposure to %d",
  243. val & 0xffff);
  244. /* The 6 MSBs */
  245. i2c_data = (val >> 10) & 0x3f;
  246. err = m5602_write_sensor(sd, OV9650_AECHM,
  247. &i2c_data, 1);
  248. if (err < 0)
  249. return err;
  250. /* The 8 middle bits */
  251. i2c_data = (val >> 2) & 0xff;
  252. err = m5602_write_sensor(sd, OV9650_AECH,
  253. &i2c_data, 1);
  254. if (err < 0)
  255. return err;
  256. /* The 2 LSBs */
  257. i2c_data = val & 0x03;
  258. err = m5602_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
  259. return err;
  260. }
  261. int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  262. {
  263. int err;
  264. u8 i2c_data;
  265. struct sd *sd = (struct sd *) gspca_dev;
  266. m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  267. *val = (i2c_data & 0x03) << 8;
  268. err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  269. *val |= i2c_data;
  270. PDEBUG(D_V4L2, "Read gain %d", *val);
  271. return err;
  272. }
  273. int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  274. {
  275. int err;
  276. u8 i2c_data;
  277. struct sd *sd = (struct sd *) gspca_dev;
  278. /* The 2 MSB */
  279. /* Read the OV9650_VREF register first to avoid
  280. corrupting the VREF high and low bits */
  281. m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  282. /* Mask away all uninteresting bits */
  283. i2c_data = ((val & 0x0300) >> 2) |
  284. (i2c_data & 0x3F);
  285. err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  286. /* The 8 LSBs */
  287. i2c_data = val & 0xff;
  288. err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  289. return err;
  290. }
  291. int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  292. {
  293. int err;
  294. u8 i2c_data;
  295. struct sd *sd = (struct sd *) gspca_dev;
  296. err = m5602_read_sensor(sd, OV9650_RED, &i2c_data, 1);
  297. *val = i2c_data;
  298. PDEBUG(D_V4L2, "Read red gain %d", *val);
  299. return err;
  300. }
  301. int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  302. {
  303. int err;
  304. u8 i2c_data;
  305. struct sd *sd = (struct sd *) gspca_dev;
  306. PDEBUG(D_V4L2, "Set red gain to %d",
  307. val & 0xff);
  308. i2c_data = val & 0xff;
  309. err = m5602_write_sensor(sd, OV9650_RED, &i2c_data, 1);
  310. return err;
  311. }
  312. int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  313. {
  314. int err;
  315. u8 i2c_data;
  316. struct sd *sd = (struct sd *) gspca_dev;
  317. err = m5602_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  318. *val = i2c_data;
  319. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  320. return err;
  321. }
  322. int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  323. {
  324. int err;
  325. u8 i2c_data;
  326. struct sd *sd = (struct sd *) gspca_dev;
  327. PDEBUG(D_V4L2, "Set blue gain to %d",
  328. val & 0xff);
  329. i2c_data = val & 0xff;
  330. err = m5602_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  331. return err;
  332. }
  333. int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  334. {
  335. int err;
  336. u8 i2c_data;
  337. struct sd *sd = (struct sd *) gspca_dev;
  338. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  339. if (dmi_check_system(ov9650_flip_dmi_table))
  340. *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
  341. else
  342. *val = (i2c_data & OV9650_HFLIP) >> 5;
  343. PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
  344. return err;
  345. }
  346. int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  347. {
  348. int err;
  349. u8 i2c_data;
  350. struct sd *sd = (struct sd *) gspca_dev;
  351. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  352. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  353. if (err < 0)
  354. return err;
  355. if (dmi_check_system(ov9650_flip_dmi_table))
  356. i2c_data = ((i2c_data & 0xdf) |
  357. (((val ? 0 : 1) & 0x01) << 5));
  358. else
  359. i2c_data = ((i2c_data & 0xdf) |
  360. ((val & 0x01) << 5));
  361. err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  362. return err;
  363. }
  364. int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  365. {
  366. int err;
  367. u8 i2c_data;
  368. struct sd *sd = (struct sd *) gspca_dev;
  369. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  370. if (dmi_check_system(ov9650_flip_dmi_table))
  371. *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
  372. else
  373. *val = (i2c_data & 0x10) >> 4;
  374. PDEBUG(D_V4L2, "Read vertical flip %d", *val);
  375. return err;
  376. }
  377. int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  378. {
  379. int err;
  380. u8 i2c_data;
  381. struct sd *sd = (struct sd *) gspca_dev;
  382. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  383. err = m5602_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  384. if (err < 0)
  385. return err;
  386. if (dmi_check_system(ov9650_flip_dmi_table))
  387. i2c_data = ((i2c_data & 0xef) |
  388. (((val ? 0 : 1) & 0x01) << 4));
  389. else
  390. i2c_data = ((i2c_data & 0xef) |
  391. ((val & 0x01) << 4));
  392. err = m5602_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  393. return err;
  394. }
  395. int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
  396. {
  397. int err;
  398. u8 i2c_data;
  399. struct sd *sd = (struct sd *) gspca_dev;
  400. err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  401. if (err < 0)
  402. return err;
  403. *val = (i2c_data & 0x03) << 8;
  404. err = m5602_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  405. *val |= i2c_data;
  406. PDEBUG(D_V4L2, "Read gain %d", *val);
  407. return err;
  408. }
  409. int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  410. {
  411. int err;
  412. u8 i2c_data;
  413. struct sd *sd = (struct sd *) gspca_dev;
  414. PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
  415. /* Read the OV9650_VREF register first to avoid
  416. corrupting the VREF high and low bits */
  417. err = m5602_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  418. if (err < 0)
  419. return err;
  420. /* Mask away all uninteresting bits */
  421. i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
  422. err = m5602_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  423. if (err < 0)
  424. return err;
  425. /* The 8 LSBs */
  426. i2c_data = val & 0xff;
  427. err = m5602_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  428. return err;
  429. }
  430. int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
  431. {
  432. int err;
  433. u8 i2c_data;
  434. struct sd *sd = (struct sd *) gspca_dev;
  435. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  436. *val = (i2c_data & OV9650_AWB_EN) >> 1;
  437. PDEBUG(D_V4L2, "Read auto white balance %d", *val);
  438. return err;
  439. }
  440. int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
  441. {
  442. int err;
  443. u8 i2c_data;
  444. struct sd *sd = (struct sd *) gspca_dev;
  445. PDEBUG(D_V4L2, "Set auto white balance to %d", val);
  446. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  447. if (err < 0)
  448. return err;
  449. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  450. err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  451. return err;
  452. }
  453. int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
  454. {
  455. int err;
  456. u8 i2c_data;
  457. struct sd *sd = (struct sd *) gspca_dev;
  458. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  459. *val = (i2c_data & OV9650_AGC_EN) >> 2;
  460. PDEBUG(D_V4L2, "Read auto gain control %d", *val);
  461. return err;
  462. }
  463. int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  464. {
  465. int err;
  466. u8 i2c_data;
  467. struct sd *sd = (struct sd *) gspca_dev;
  468. PDEBUG(D_V4L2, "Set auto gain control to %d", val);
  469. err = m5602_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  470. if (err < 0)
  471. return err;
  472. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  473. err = m5602_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  474. return err;
  475. }
  476. static void ov9650_dump_registers(struct sd *sd)
  477. {
  478. int address;
  479. info("Dumping the ov9650 register state");
  480. for (address = 0; address < 0xa9; address++) {
  481. u8 value;
  482. m5602_read_sensor(sd, address, &value, 1);
  483. info("register 0x%x contains 0x%x",
  484. address, value);
  485. }
  486. info("ov9650 register state dump complete");
  487. info("Probing for which registers that are read/write");
  488. for (address = 0; address < 0xff; address++) {
  489. u8 old_value, ctrl_value;
  490. u8 test_value[2] = {0xff, 0xff};
  491. m5602_read_sensor(sd, address, &old_value, 1);
  492. m5602_write_sensor(sd, address, test_value, 1);
  493. m5602_read_sensor(sd, address, &ctrl_value, 1);
  494. if (ctrl_value == test_value[0])
  495. info("register 0x%x is writeable", address);
  496. else
  497. info("register 0x%x is read only", address);
  498. /* Restore original value */
  499. m5602_write_sensor(sd, address, &old_value, 1);
  500. }
  501. }