m5602_ov9650.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. 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(D_CONF, "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(D_CONF, "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. sd->desc->ctrls = ov9650.ctrls;
  110. sd->desc->nctrls = ov9650.nctrls;
  111. return 0;
  112. }
  113. int ov9650_init(struct sd *sd)
  114. {
  115. int i, err = 0;
  116. u8 data;
  117. if (dump_sensor)
  118. ov9650_dump_registers(sd);
  119. for (i = 0; i < ARRAY_SIZE(init_ov9650) && !err; i++) {
  120. data = init_ov9650[i][2];
  121. if (init_ov9650[i][0] == SENSOR)
  122. err = ov9650_write_sensor(sd, init_ov9650[i][1],
  123. &data, 1);
  124. else
  125. err = m5602_write_bridge(sd, init_ov9650[i][1], data);
  126. }
  127. if (!err && dmi_check_system(ov9650_flip_dmi_table)) {
  128. info("vflip quirk active");
  129. data = 0x30;
  130. err = ov9650_write_sensor(sd, OV9650_MVFP, &data, 1);
  131. }
  132. return (err < 0) ? err : 0;
  133. }
  134. int ov9650_power_down(struct sd *sd)
  135. {
  136. int i;
  137. for (i = 0; i < ARRAY_SIZE(power_down_ov9650); i++) {
  138. u8 data = power_down_ov9650[i][2];
  139. if (power_down_ov9650[i][0] == SENSOR)
  140. ov9650_write_sensor(sd,
  141. power_down_ov9650[i][1], &data, 1);
  142. else
  143. m5602_write_bridge(sd, power_down_ov9650[i][1], data);
  144. }
  145. return 0;
  146. }
  147. int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  148. {
  149. struct sd *sd = (struct sd *) gspca_dev;
  150. u8 i2c_data;
  151. int err;
  152. err = ov9650_read_sensor(sd, OV9650_COM1, &i2c_data, 1);
  153. if (err < 0)
  154. goto out;
  155. *val = i2c_data & 0x03;
  156. err = ov9650_read_sensor(sd, OV9650_AECH, &i2c_data, 1);
  157. if (err < 0)
  158. goto out;
  159. *val |= (i2c_data << 2);
  160. err = ov9650_read_sensor(sd, OV9650_AECHM, &i2c_data, 1);
  161. if (err < 0)
  162. goto out;
  163. *val |= (i2c_data & 0x3f) << 10;
  164. PDEBUG(D_V4L2, "Read exposure %d", *val);
  165. out:
  166. return (err < 0) ? err : 0;
  167. }
  168. int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  169. {
  170. struct sd *sd = (struct sd *) gspca_dev;
  171. u8 i2c_data;
  172. int err;
  173. PDEBUG(D_V4L2, "Set exposure to %d",
  174. val & 0xffff);
  175. /* The 6 MSBs */
  176. i2c_data = (val >> 10) & 0x3f;
  177. err = ov9650_write_sensor(sd, OV9650_AECHM,
  178. &i2c_data, 1);
  179. if (err < 0)
  180. goto out;
  181. /* The 8 middle bits */
  182. i2c_data = (val >> 2) & 0xff;
  183. err = ov9650_write_sensor(sd, OV9650_AECH,
  184. &i2c_data, 1);
  185. if (err < 0)
  186. goto out;
  187. /* The 2 LSBs */
  188. i2c_data = val & 0x03;
  189. err = ov9650_write_sensor(sd, OV9650_COM1, &i2c_data, 1);
  190. out:
  191. return (err < 0) ? err : 0;
  192. }
  193. int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  194. {
  195. int err;
  196. u8 i2c_data;
  197. struct sd *sd = (struct sd *) gspca_dev;
  198. ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  199. *val = (i2c_data & 0x03) << 8;
  200. err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  201. *val |= i2c_data;
  202. PDEBUG(D_V4L2, "Read gain %d", *val);
  203. return (err < 0) ? err : 0;
  204. }
  205. int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  206. {
  207. int err;
  208. u8 i2c_data;
  209. struct sd *sd = (struct sd *) gspca_dev;
  210. /* The 2 MSB */
  211. /* Read the OV9650_VREF register first to avoid
  212. corrupting the VREF high and low bits */
  213. ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  214. /* Mask away all uninteresting bits */
  215. i2c_data = ((val & 0x0300) >> 2) |
  216. (i2c_data & 0x3F);
  217. err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  218. /* The 8 LSBs */
  219. i2c_data = val & 0xff;
  220. err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  221. return (err < 0) ? err : 0;
  222. }
  223. int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val)
  224. {
  225. int err;
  226. u8 i2c_data;
  227. struct sd *sd = (struct sd *) gspca_dev;
  228. err = ov9650_read_sensor(sd, OV9650_RED, &i2c_data, 1);
  229. *val = i2c_data;
  230. PDEBUG(D_V4L2, "Read red gain %d", *val);
  231. return (err < 0) ? err : 0;
  232. }
  233. int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  234. {
  235. int err;
  236. u8 i2c_data;
  237. struct sd *sd = (struct sd *) gspca_dev;
  238. PDEBUG(D_V4L2, "Set red gain to %d",
  239. val & 0xff);
  240. i2c_data = val & 0xff;
  241. err = ov9650_write_sensor(sd, OV9650_RED, &i2c_data, 1);
  242. return (err < 0) ? err : 0;
  243. }
  244. int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val)
  245. {
  246. int err;
  247. u8 i2c_data;
  248. struct sd *sd = (struct sd *) gspca_dev;
  249. err = ov9650_read_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  250. *val = i2c_data;
  251. PDEBUG(D_V4L2, "Read blue gain %d", *val);
  252. return (err < 0) ? err : 0;
  253. }
  254. int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  255. {
  256. int err;
  257. u8 i2c_data;
  258. struct sd *sd = (struct sd *) gspca_dev;
  259. PDEBUG(D_V4L2, "Set blue gain to %d",
  260. val & 0xff);
  261. i2c_data = val & 0xff;
  262. err = ov9650_write_sensor(sd, OV9650_BLUE, &i2c_data, 1);
  263. return (err < 0) ? err : 0;
  264. }
  265. int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  266. {
  267. int err;
  268. u8 i2c_data;
  269. struct sd *sd = (struct sd *) gspca_dev;
  270. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  271. if (dmi_check_system(ov9650_flip_dmi_table))
  272. *val = ((i2c_data & OV9650_HFLIP) >> 5) ? 0 : 1;
  273. else
  274. *val = (i2c_data & OV9650_HFLIP) >> 5;
  275. PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
  276. return (err < 0) ? err : 0;
  277. }
  278. int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  279. {
  280. int err;
  281. u8 i2c_data;
  282. struct sd *sd = (struct sd *) gspca_dev;
  283. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  284. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  285. if (err < 0)
  286. goto out;
  287. if (dmi_check_system(ov9650_flip_dmi_table))
  288. i2c_data = ((i2c_data & 0xdf) |
  289. (((val ? 0 : 1) & 0x01) << 5));
  290. else
  291. i2c_data = ((i2c_data & 0xdf) |
  292. ((val & 0x01) << 5));
  293. err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  294. out:
  295. return (err < 0) ? err : 0;
  296. }
  297. int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  298. {
  299. int err;
  300. u8 i2c_data;
  301. struct sd *sd = (struct sd *) gspca_dev;
  302. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  303. if (dmi_check_system(ov9650_flip_dmi_table))
  304. *val = ((i2c_data & 0x10) >> 4) ? 0 : 1;
  305. else
  306. *val = (i2c_data & 0x10) >> 4;
  307. PDEBUG(D_V4L2, "Read vertical flip %d", *val);
  308. return (err < 0) ? err : 0;
  309. }
  310. int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  311. {
  312. int err;
  313. u8 i2c_data;
  314. struct sd *sd = (struct sd *) gspca_dev;
  315. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  316. err = ov9650_read_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  317. if (err < 0)
  318. goto out;
  319. if (dmi_check_system(ov9650_flip_dmi_table))
  320. i2c_data = ((i2c_data & 0xef) |
  321. (((val ? 0 : 1) & 0x01) << 4));
  322. else
  323. i2c_data = ((i2c_data & 0xef) |
  324. ((val & 0x01) << 4));
  325. err = ov9650_write_sensor(sd, OV9650_MVFP, &i2c_data, 1);
  326. out:
  327. return (err < 0) ? err : 0;
  328. }
  329. int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val)
  330. {
  331. int err;
  332. u8 i2c_data;
  333. struct sd *sd = (struct sd *) gspca_dev;
  334. err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  335. if (err < 0)
  336. goto out;
  337. *val = (i2c_data & 0x03) << 8;
  338. err = ov9650_read_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  339. *val |= i2c_data;
  340. PDEBUG(D_V4L2, "Read gain %d", *val);
  341. out:
  342. return (err < 0) ? err : 0;
  343. }
  344. int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val)
  345. {
  346. int err;
  347. u8 i2c_data;
  348. struct sd *sd = (struct sd *) gspca_dev;
  349. PDEBUG(D_V4L2, "Set gain to %d", val & 0x3ff);
  350. /* Read the OV9650_VREF register first to avoid
  351. corrupting the VREF high and low bits */
  352. err = ov9650_read_sensor(sd, OV9650_VREF, &i2c_data, 1);
  353. if (err < 0)
  354. goto out;
  355. /* Mask away all uninteresting bits */
  356. i2c_data = ((val & 0x0300) >> 2) | (i2c_data & 0x3F);
  357. err = ov9650_write_sensor(sd, OV9650_VREF, &i2c_data, 1);
  358. if (err < 0)
  359. goto out;
  360. /* The 8 LSBs */
  361. i2c_data = val & 0xff;
  362. err = ov9650_write_sensor(sd, OV9650_GAIN, &i2c_data, 1);
  363. out:
  364. return (err < 0) ? err : 0;
  365. }
  366. int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val)
  367. {
  368. int err;
  369. u8 i2c_data;
  370. struct sd *sd = (struct sd *) gspca_dev;
  371. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  372. *val = (i2c_data & OV9650_AWB_EN) >> 1;
  373. PDEBUG(D_V4L2, "Read auto white balance %d", *val);
  374. return (err < 0) ? err : 0;
  375. }
  376. int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val)
  377. {
  378. int err;
  379. u8 i2c_data;
  380. struct sd *sd = (struct sd *) gspca_dev;
  381. PDEBUG(D_V4L2, "Set auto white balance to %d", val);
  382. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  383. if (err < 0)
  384. goto out;
  385. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  386. err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  387. out:
  388. return (err < 0) ? err : 0;
  389. }
  390. int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val)
  391. {
  392. int err;
  393. u8 i2c_data;
  394. struct sd *sd = (struct sd *) gspca_dev;
  395. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  396. *val = (i2c_data & OV9650_AGC_EN) >> 2;
  397. PDEBUG(D_V4L2, "Read auto gain control %d", *val);
  398. return (err < 0) ? err : 0;
  399. }
  400. int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  401. {
  402. int err;
  403. u8 i2c_data;
  404. struct sd *sd = (struct sd *) gspca_dev;
  405. PDEBUG(D_V4L2, "Set auto gain control to %d", val);
  406. err = ov9650_read_sensor(sd, OV9650_COM8, &i2c_data, 1);
  407. if (err < 0)
  408. goto out;
  409. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  410. err = ov9650_write_sensor(sd, OV9650_COM8, &i2c_data, 1);
  411. out:
  412. return (err < 0) ? err : 0;
  413. }
  414. void ov9650_dump_registers(struct sd *sd)
  415. {
  416. int address;
  417. info("Dumping the ov9650 register state");
  418. for (address = 0; address < 0xa9; address++) {
  419. u8 value;
  420. ov9650_read_sensor(sd, address, &value, 1);
  421. info("register 0x%x contains 0x%x",
  422. address, value);
  423. }
  424. info("ov9650 register state dump complete");
  425. info("Probing for which registers that are read/write");
  426. for (address = 0; address < 0xff; address++) {
  427. u8 old_value, ctrl_value;
  428. u8 test_value[2] = {0xff, 0xff};
  429. ov9650_read_sensor(sd, address, &old_value, 1);
  430. ov9650_write_sensor(sd, address, test_value, 1);
  431. ov9650_read_sensor(sd, address, &ctrl_value, 1);
  432. if (ctrl_value == test_value[0])
  433. info("register 0x%x is writeable", address);
  434. else
  435. info("register 0x%x is read only", address);
  436. /* Restore original value */
  437. ov9650_write_sensor(sd, address, &old_value, 1);
  438. }
  439. }