m5602_ov9650.c 14 KB

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