m5602_ov9650.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Driver for the ov9650 sensor
  3. *
  4. * Copyright (C) 2008 Erik Andren
  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. int ov9650_read_sensor(struct sd *sd, const u8 address,
  20. u8 *i2c_data, const u8 len)
  21. {
  22. int err, i;
  23. /* The ov9650 registers have a max depth of one byte */
  24. if (len > 1 || !len)
  25. return -EINVAL;
  26. do {
  27. err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, i2c_data);
  28. } while ((*i2c_data & I2C_BUSY) && !err);
  29. m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
  30. ov9650.i2c_slave_id);
  31. m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
  32. m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x10 + len);
  33. m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
  34. for (i = 0; i < len; i++) {
  35. err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
  36. PDEBUG(DBG_TRACE, "Reading sensor register "
  37. "0x%x containing 0x%x ", address, *i2c_data);
  38. }
  39. return (err < 0) ? err : 0;
  40. }
  41. int ov9650_write_sensor(struct sd *sd, const u8 address,
  42. u8 *i2c_data, const u8 len)
  43. {
  44. int err, i;
  45. u8 *p;
  46. struct usb_device *udev = sd->gspca_dev.dev;
  47. __u8 *buf = sd->gspca_dev.usb_buf;
  48. /* The ov9650 only supports one byte writes */
  49. if (len > 1 || !len)
  50. return -EINVAL;
  51. memcpy(buf, sensor_urb_skeleton,
  52. sizeof(sensor_urb_skeleton));
  53. buf[11] = sd->sensor->i2c_slave_id;
  54. buf[15] = address;
  55. /* Special case larger sensor writes */
  56. p = buf + 16;
  57. /* Copy a four byte write sequence for each byte to be written to */
  58. for (i = 0; i < len; i++) {
  59. memcpy(p, sensor_urb_skeleton + 16, 4);
  60. p[3] = i2c_data[i];
  61. p += 4;
  62. PDEBUG(DBG_TRACE, "Writing sensor register 0x%x with 0x%x",
  63. address, i2c_data[i]);
  64. }
  65. /* Copy the tailer */
  66. memcpy(p, sensor_urb_skeleton + 20, 4);
  67. /* Set the total length */
  68. p[3] = 0x10 + len;
  69. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  70. 0x04, 0x40, 0x19,
  71. 0x0000, buf,
  72. 20 + len * 4, M5602_URB_MSG_TIMEOUT);
  73. return (err < 0) ? err : 0;
  74. }
  75. int ov9650_probe(struct sd *sd)
  76. {
  77. u8 prod_id = 0, ver_id = 0, i;
  78. if (force_sensor) {
  79. if (force_sensor == OV9650_SENSOR) {
  80. info("Forcing an %s sensor", ov9650.name);
  81. goto sensor_found;
  82. }
  83. /* If we want to force another sensor,
  84. don't try to probe this one */
  85. return -ENODEV;
  86. }
  87. info("Probing for an ov9650 sensor");
  88. /* Run the pre-init to actually probe the unit */
  89. for (i = 0; i < ARRAY_SIZE(preinit_ov9650); i++) {
  90. u8 data = preinit_ov9650[i][2];
  91. if (preinit_ov9650[i][0] == SENSOR)
  92. ov9650_write_sensor(sd,
  93. preinit_ov9650[i][1], &data, 1);
  94. else
  95. m5602_write_bridge(sd, preinit_ov9650[i][1], data);
  96. }
  97. if (ov9650_read_sensor(sd, OV9650_PID, &prod_id, 1))
  98. return -ENODEV;
  99. if (ov9650_read_sensor(sd, OV9650_VER, &ver_id, 1))
  100. return -ENODEV;
  101. if ((prod_id == 0x96) && (ver_id == 0x52)) {
  102. info("Detected an ov9650 sensor");
  103. goto sensor_found;
  104. }
  105. return -ENODEV;
  106. sensor_found:
  107. sd->gspca_dev.cam.cam_mode = ov9650.modes;
  108. sd->gspca_dev.cam.nmodes = ov9650.nmodes;
  109. return 0;
  110. }
  111. int ov9650_init(struct sd *sd)
  112. {
  113. int i, err = 0;
  114. u8 data;
  115. if (dump_sensor)
  116. ov9650_dump_registers(sd);
  117. for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
  118. data = init_ov9650[i][2];
  119. if (init_ov9650[i][0] == SENSOR)
  120. err = ov9650_write_sensor(sd, init_ov9650[i][1],
  121. &data, 1);
  122. else
  123. err = m5602_write_bridge(sd, init_ov9650[i][1], data);
  124. }
  125. if (!err && dmi_check_system(ov9650_flip_dmi_table)) {
  126. info("vflip quirk active");
  127. data = 0x30;
  128. err = ov9650_write_sensor(sd, OV9650_MVFP, &data, 1);
  129. }
  130. return (err < 0) ? err : 0;
  131. }
  132. int ov9650_power_down(struct sd *sd)
  133. {
  134. int i;
  135. for (i = 0; i < ARRAY_SIZE(power_down_ov9650); i++) {
  136. u8 data = power_down_ov9650[i][2];
  137. if (power_down_ov9650[i][0] == SENSOR)
  138. ov9650_write_sensor(sd,
  139. power_down_ov9650[i][1], &data, 1);
  140. else
  141. m5602_write_bridge(sd, power_down_ov9650[i][1], data);
  142. }
  143. return 0;
  144. }
  145. int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  146. {
  147. struct sd *sd = (struct sd *) gspca_dev;
  148. u8 i2c_data;
  149. int err;
  150. err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
  151. if (err < 0)
  152. goto out;
  153. *val = i2c_data & 0x03;
  154. err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
  155. if (err < 0)
  156. goto out;
  157. *val |= (i2c_data << 2);
  158. err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
  159. if (err < 0)
  160. goto out;
  161. *val |= (i2c_data & 0x3f) << 10;
  162. PDEBUG(DBG_V4L2_CID, "Read exposure %d", *val);
  163. out:
  164. return (err < 0) ? err : 0;
  165. }
  166. int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  167. {
  168. struct sd *sd = (struct sd *) gspca_dev;
  169. u8 i2c_data;
  170. int err;
  171. PDEBUG(DBG_V4L2_CID, "Set exposure to %d",
  172. val & 0xffff);
  173. /* The 6 MSBs */
  174. i2c_data = (val >> 10) & 0x3f;
  175. err = ov9650_write_sensor(sd, OV9650_AECHM,
  176. &i2c_data, 1);
  177. if (err < 0)
  178. goto out;
  179. /* The 8 middle bits */
  180. i2c_data = (val >> 2) & 0xff;
  181. err = ov9650_write_sensor(sd, OV9650_AECH,
  182. &i2c_data, 1);
  183. if (err < 0)
  184. goto out;
  185. /* The 2 LSBs */
  186. i2c_data = val & 0x03;
  187. err = ov9650_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
  188. out:
  189. return (err < 0) ? err : 0;
  190. }
  191. int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  192. {
  193. int err;
  194. u8 i2c_data;
  195. struct sd *sd = (struct sd *) gspca_dev;
  196. ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  197. *val = (i2c_data & 0x03) << 8;
  198. err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  199. *val |= i2c_data;
  200. PDEBUG(DBG_V4L2_CID, "Read gain %d", *val);
  201. return (err < 0) ? err : 0;
  202. }
  203. int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  204. {
  205. int err;
  206. u8 i2c_data;
  207. struct sd *sd = (struct sd *) gspca_dev;
  208. /* The 2 MSB */
  209. /* Read the OV9650_VREF register first to avoid
  210. corrupting the VREF high and low bits */
  211. ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  212. /* Mask away all uninteresting bits */
  213. i2c_data = ((val & 0x0300) >> 2) |
  214. (i2c_data & 0x3F);
  215. err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  216. /* The 8 LSBs */
  217. i2c_data = val & 0xff;
  218. err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  219. return (err < 0) ? err : 0;
  220. }
  221. int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  222. {
  223. int err;
  224. u8 i2c_data;
  225. struct sd *sd = (struct sd *) gspca_dev;
  226. err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
  227. *val = i2c_data;
  228. PDEBUG(DBG_V4L2_CID, "Read red gain %d", *val);
  229. return (err < 0) ? err : 0;
  230. }
  231. int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  232. {
  233. int err;
  234. u8 i2c_data;
  235. struct sd *sd = (struct sd *) gspca_dev;
  236. PDEBUG(DBG_V4L2_CID, "Set red gain to %d",
  237. val & 0xff);
  238. i2c_data = val & 0xff;
  239. err = ov9650_write_sensor(sd, OV9650_RED, &i2c_data, 1);
  240. return (err < 0) ? err : 0;
  241. }
  242. int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  243. {
  244. int err;
  245. u8 i2c_data;
  246. struct sd *sd = (struct sd *) gspca_dev;
  247. err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  248. *val = i2c_data;
  249. PDEBUG(DBG_V4L2_CID, "Read blue gain %d", *val);
  250. return (err < 0) ? err : 0;
  251. }
  252. int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  253. {
  254. int err;
  255. u8 i2c_data;
  256. struct sd *sd = (struct sd *) gspca_dev;
  257. PDEBUG(DBG_V4L2_CID, "Set blue gain to %d",
  258. val & 0xff);
  259. i2c_data = val & 0xff;
  260. err = ov9650_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  261. return (err < 0) ? err : 0;
  262. }
  263. int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  264. {
  265. int err;
  266. u8 i2c_data;
  267. struct sd *sd = (struct sd *) gspca_dev;
  268. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  269. if (dmi_check_system(ov9650_flip_dmi_table))
  270. *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
  271. else
  272. *val = (i2c_data & OV9650_HFLIP) >> 5;
  273. PDEBUG(DBG_V4L2_CID, "Read horizontal flip %d", *val);
  274. return (err < 0) ? err : 0;
  275. }
  276. int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  277. {
  278. int err;
  279. u8 i2c_data;
  280. struct sd *sd = (struct sd *) gspca_dev;
  281. PDEBUG(DBG_V4L2_CID, "Set horizontal flip to %d", val);
  282. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  283. if (err < 0)
  284. goto out;
  285. if (dmi_check_system(ov9650_flip_dmi_table))
  286. i2c_data = ((i2c_data & 0xdf) |
  287. (((val ? 0 : 1) & 0x01) << 5));
  288. else
  289. i2c_data = ((i2c_data & 0xdf) |
  290. ((val & 0x01) << 5));
  291. err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  292. out:
  293. return (err < 0) ? err : 0;
  294. }
  295. int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  296. {
  297. int err;
  298. u8 i2c_data;
  299. struct sd *sd = (struct sd *) gspca_dev;
  300. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  301. if (dmi_check_system(ov9650_flip_dmi_table))
  302. *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
  303. else
  304. *val = (i2c_data & 0x10) >> 4;
  305. PDEBUG(DBG_V4L2_CID, "Read vertical flip %d", *val);
  306. return (err < 0) ? err : 0;
  307. }
  308. int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  309. {
  310. int err;
  311. u8 i2c_data;
  312. struct sd *sd = (struct sd *) gspca_dev;
  313. PDEBUG(DBG_V4L2_CID, "Set vertical flip to %d", val);
  314. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  315. if (err < 0)
  316. goto out;
  317. if (dmi_check_system(ov9650_flip_dmi_table))
  318. i2c_data = ((i2c_data & 0xef) |
  319. (((val ? 0 : 1) & 0x01) << 4));
  320. else
  321. i2c_data = ((i2c_data & 0xef) |
  322. ((val & 0x01) << 4));
  323. err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  324. out:
  325. return (err < 0) ? err : 0;
  326. }
  327. int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
  328. {
  329. int err;
  330. u8 i2c_data;
  331. struct sd *sd = (struct sd *) gspca_dev;
  332. err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  333. if (err < 0)
  334. goto out;
  335. *val = (i2c_data & 0x03) << 8;
  336. err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  337. *val |= i2c_data;
  338. PDEBUG(DBG_V4L2_CID, "Read gain %d", *val);
  339. out:
  340. return (err < 0) ? err : 0;
  341. }
  342. int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  343. {
  344. int err;
  345. u8 i2c_data;
  346. struct sd *sd = (struct sd *) gspca_dev;
  347. PDEBUG(DBG_V4L2_CID, "Set gain to %d", val & 0x3ff);
  348. /* Read the OV9650_VREF register first to avoid
  349. corrupting the VREF high and low bits */
  350. err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  351. if (err < 0)
  352. goto out;
  353. /* Mask away all uninteresting bits */
  354. i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
  355. err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  356. if (err < 0)
  357. goto out;
  358. /* The 8 LSBs */
  359. i2c_data = val & 0xff;
  360. err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  361. out:
  362. return (err < 0) ? err : 0;
  363. }
  364. int ov9650_get_auto_white_balance(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 = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  370. *val = (i2c_data & OV9650_AWB_EN) >> 1;
  371. PDEBUG(DBG_V4L2_CID, "Read auto white balance %d", *val);
  372. return (err < 0) ? err : 0;
  373. }
  374. int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
  375. {
  376. int err;
  377. u8 i2c_data;
  378. struct sd *sd = (struct sd *) gspca_dev;
  379. PDEBUG(DBG_V4L2_CID, "Set auto white balance to %d", val);
  380. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  381. if (err < 0)
  382. goto out;
  383. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  384. err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  385. out:
  386. return (err < 0) ? err : 0;
  387. }
  388. int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
  389. {
  390. int err;
  391. u8 i2c_data;
  392. struct sd *sd = (struct sd *) gspca_dev;
  393. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  394. *val = (i2c_data & OV9650_AGC_EN) >> 2;
  395. PDEBUG(DBG_V4L2_CID, "Read auto gain control %d", *val);
  396. return (err < 0) ? err : 0;
  397. }
  398. int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  399. {
  400. int err;
  401. u8 i2c_data;
  402. struct sd *sd = (struct sd *) gspca_dev;
  403. PDEBUG(DBG_V4L2_CID, "Set auto gain control to %d", val);
  404. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  405. if (err < 0)
  406. goto out;
  407. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  408. err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  409. out:
  410. return (err < 0) ? err : 0;
  411. }
  412. void ov9650_dump_registers(struct sd *sd)
  413. {
  414. int address;
  415. info("Dumping the ov9650 register state");
  416. for (address = 0; address < 0xa9; address++) {
  417. u8 value;
  418. ov9650_read_sensor(sd, address, &value, 1);
  419. info("register 0x%x contains 0x%x",
  420. address, value);
  421. }
  422. info("ov9650 register state dump complete");
  423. info("Probing for which registers that are read/write");
  424. for (address = 0; address < 0xff; address++) {
  425. u8 old_value, ctrl_value;
  426. u8 test_value[2] = {0xff, 0xff};
  427. ov9650_read_sensor(sd, address, &old_value, 1);
  428. ov9650_write_sensor(sd, address, test_value, 1);
  429. ov9650_read_sensor(sd, address, &ctrl_value, 1);
  430. if (ctrl_value == test_value[0])
  431. info("register 0x%x is writeable", address);
  432. else
  433. info("register 0x%x is read only", address);
  434. /* Restore original value */
  435. ov9650_write_sensor(sd, address, &old_value, 1);
  436. }
  437. }