sn9c102_ov7630.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /***************************************************************************
  2. * Plug-in for OV7630 image sensor connected to the SN9C10x PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2005-2006 by Luca Risolia <luca.risolia@studio.unibo.it> *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the Free Software *
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  20. ***************************************************************************/
  21. #include "sn9c102_sensor.h"
  22. static struct sn9c102_sensor ov7630;
  23. static int ov7630_init(struct sn9c102_device* cam)
  24. {
  25. int err = 0;
  26. err += sn9c102_write_reg(cam, 0x00, 0x14);
  27. err += sn9c102_write_reg(cam, 0x60, 0x17);
  28. err += sn9c102_write_reg(cam, 0x0f, 0x18);
  29. err += sn9c102_write_reg(cam, 0x50, 0x19);
  30. err += sn9c102_i2c_write(cam, 0x12, 0x80);
  31. err += sn9c102_i2c_write(cam, 0x11, 0x01);
  32. err += sn9c102_i2c_write(cam, 0x15, 0x34);
  33. err += sn9c102_i2c_write(cam, 0x16, 0x03);
  34. err += sn9c102_i2c_write(cam, 0x17, 0x1c);
  35. err += sn9c102_i2c_write(cam, 0x18, 0xbd);
  36. err += sn9c102_i2c_write(cam, 0x19, 0x06);
  37. err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
  38. err += sn9c102_i2c_write(cam, 0x1b, 0x04);
  39. err += sn9c102_i2c_write(cam, 0x20, 0xf6);
  40. err += sn9c102_i2c_write(cam, 0x23, 0xee);
  41. err += sn9c102_i2c_write(cam, 0x26, 0xa0);
  42. err += sn9c102_i2c_write(cam, 0x27, 0x9a);
  43. err += sn9c102_i2c_write(cam, 0x28, 0xa0);
  44. err += sn9c102_i2c_write(cam, 0x29, 0x30);
  45. err += sn9c102_i2c_write(cam, 0x2a, 0xa0);
  46. err += sn9c102_i2c_write(cam, 0x2b, 0x1f);
  47. err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
  48. err += sn9c102_i2c_write(cam, 0x30, 0x24);
  49. err += sn9c102_i2c_write(cam, 0x32, 0x86);
  50. err += sn9c102_i2c_write(cam, 0x60, 0xa9);
  51. err += sn9c102_i2c_write(cam, 0x61, 0x42);
  52. err += sn9c102_i2c_write(cam, 0x65, 0x00);
  53. err += sn9c102_i2c_write(cam, 0x69, 0x38);
  54. err += sn9c102_i2c_write(cam, 0x6f, 0x88);
  55. err += sn9c102_i2c_write(cam, 0x70, 0x0b);
  56. err += sn9c102_i2c_write(cam, 0x71, 0x00);
  57. err += sn9c102_i2c_write(cam, 0x74, 0x21);
  58. err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
  59. return err;
  60. }
  61. static int ov7630_set_ctrl(struct sn9c102_device* cam,
  62. const struct v4l2_control* ctrl)
  63. {
  64. int err = 0;
  65. switch (ctrl->id) {
  66. case V4L2_CID_EXPOSURE:
  67. err += sn9c102_i2c_write(cam, 0x10, ctrl->value >> 2);
  68. err += sn9c102_i2c_write(cam, 0x76, ctrl->value & 0x03);
  69. break;
  70. case V4L2_CID_RED_BALANCE:
  71. err += sn9c102_i2c_write(cam, 0x02, ctrl->value);
  72. break;
  73. case V4L2_CID_BLUE_BALANCE:
  74. err += sn9c102_i2c_write(cam, 0x01, ctrl->value);
  75. break;
  76. case V4L2_CID_GAIN:
  77. err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
  78. break;
  79. case V4L2_CID_CONTRAST:
  80. err += ctrl->value ? sn9c102_i2c_write(cam, 0x05,
  81. (ctrl->value-1) | 0x20)
  82. : sn9c102_i2c_write(cam, 0x05, 0x00);
  83. break;
  84. case V4L2_CID_BRIGHTNESS:
  85. err += sn9c102_i2c_write(cam, 0x06, ctrl->value);
  86. break;
  87. case V4L2_CID_SATURATION:
  88. err += sn9c102_i2c_write(cam, 0x03, ctrl->value << 4);
  89. break;
  90. case V4L2_CID_HUE:
  91. err += ctrl->value ? sn9c102_i2c_write(cam, 0x04,
  92. (ctrl->value-1) | 0x20)
  93. : sn9c102_i2c_write(cam, 0x04, 0x00);
  94. break;
  95. case V4L2_CID_DO_WHITE_BALANCE:
  96. err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
  97. break;
  98. case V4L2_CID_WHITENESS:
  99. err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
  100. break;
  101. case V4L2_CID_AUTO_WHITE_BALANCE:
  102. err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x78);
  103. break;
  104. case V4L2_CID_AUTOGAIN:
  105. err += sn9c102_i2c_write(cam, 0x13, ctrl->value);
  106. break;
  107. case V4L2_CID_VFLIP:
  108. err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7));
  109. break;
  110. case V4L2_CID_BLACK_LEVEL:
  111. err += sn9c102_i2c_write(cam, 0x25, ctrl->value);
  112. break;
  113. case SN9C102_V4L2_CID_BRIGHT_LEVEL:
  114. err += sn9c102_i2c_write(cam, 0x24, ctrl->value);
  115. break;
  116. case SN9C102_V4L2_CID_GAMMA:
  117. err += sn9c102_i2c_write(cam, 0x14, (ctrl->value << 2) | 0x80);
  118. break;
  119. case SN9C102_V4L2_CID_BAND_FILTER:
  120. err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2);
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. return err ? -EIO : 0;
  126. }
  127. static int ov7630_set_crop(struct sn9c102_device* cam,
  128. const struct v4l2_rect* rect)
  129. {
  130. struct sn9c102_sensor* s = &ov7630;
  131. int err = 0;
  132. u8 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
  133. err += sn9c102_write_reg(cam, v_start, 0x13);
  134. return err;
  135. }
  136. static int ov7630_set_pix_format(struct sn9c102_device* cam,
  137. const struct v4l2_pix_format* pix)
  138. {
  139. int err = 0;
  140. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  141. err += sn9c102_write_reg(cam, 0x20, 0x19);
  142. else
  143. err += sn9c102_write_reg(cam, 0x50, 0x19);
  144. return err;
  145. }
  146. static struct sn9c102_sensor ov7630 = {
  147. .name = "OV7630",
  148. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  149. .sysfs_ops = SN9C102_I2C_WRITE,
  150. .frequency = SN9C102_I2C_100KHZ,
  151. .interface = SN9C102_I2C_2WIRES,
  152. .i2c_slave_id = 0x21,
  153. .init = &ov7630_init,
  154. .qctrl = {
  155. {
  156. .id = V4L2_CID_GAIN,
  157. .type = V4L2_CTRL_TYPE_INTEGER,
  158. .name = "global gain",
  159. .minimum = 0x00,
  160. .maximum = 0x3f,
  161. .step = 0x01,
  162. .default_value = 0x14,
  163. .flags = 0,
  164. },
  165. {
  166. .id = V4L2_CID_HUE,
  167. .type = V4L2_CTRL_TYPE_INTEGER,
  168. .name = "hue",
  169. .minimum = 0x00,
  170. .maximum = 0x1f+1,
  171. .step = 0x01,
  172. .default_value = 0x00,
  173. .flags = 0,
  174. },
  175. {
  176. .id = V4L2_CID_SATURATION,
  177. .type = V4L2_CTRL_TYPE_INTEGER,
  178. .name = "saturation",
  179. .minimum = 0x00,
  180. .maximum = 0x0f,
  181. .step = 0x01,
  182. .default_value = 0x08,
  183. .flags = 0,
  184. },
  185. {
  186. .id = V4L2_CID_CONTRAST,
  187. .type = V4L2_CTRL_TYPE_INTEGER,
  188. .name = "contrast",
  189. .minimum = 0x00,
  190. .maximum = 0x1f+1,
  191. .step = 0x01,
  192. .default_value = 0x00,
  193. .flags = 0,
  194. },
  195. {
  196. .id = V4L2_CID_EXPOSURE,
  197. .type = V4L2_CTRL_TYPE_INTEGER,
  198. .name = "exposure",
  199. .minimum = 0x000,
  200. .maximum = 0x3ff,
  201. .step = 0x001,
  202. .default_value = 0x83<<2,
  203. .flags = 0,
  204. },
  205. {
  206. .id = V4L2_CID_RED_BALANCE,
  207. .type = V4L2_CTRL_TYPE_INTEGER,
  208. .name = "red balance",
  209. .minimum = 0x00,
  210. .maximum = 0xff,
  211. .step = 0x01,
  212. .default_value = 0x3a,
  213. .flags = 0,
  214. },
  215. {
  216. .id = V4L2_CID_BLUE_BALANCE,
  217. .type = V4L2_CTRL_TYPE_INTEGER,
  218. .name = "blue balance",
  219. .minimum = 0x00,
  220. .maximum = 0xff,
  221. .step = 0x01,
  222. .default_value = 0x77,
  223. .flags = 0,
  224. },
  225. {
  226. .id = V4L2_CID_BRIGHTNESS,
  227. .type = V4L2_CTRL_TYPE_INTEGER,
  228. .name = "brightness",
  229. .minimum = 0x00,
  230. .maximum = 0xff,
  231. .step = 0x01,
  232. .default_value = 0xa0,
  233. .flags = 0,
  234. },
  235. {
  236. .id = V4L2_CID_DO_WHITE_BALANCE,
  237. .type = V4L2_CTRL_TYPE_INTEGER,
  238. .name = "white balance background: blue",
  239. .minimum = 0x00,
  240. .maximum = 0x3f,
  241. .step = 0x01,
  242. .default_value = 0x20,
  243. .flags = 0,
  244. },
  245. {
  246. .id = V4L2_CID_WHITENESS,
  247. .type = V4L2_CTRL_TYPE_INTEGER,
  248. .name = "white balance background: red",
  249. .minimum = 0x00,
  250. .maximum = 0x3f,
  251. .step = 0x01,
  252. .default_value = 0x20,
  253. .flags = 0,
  254. },
  255. {
  256. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  257. .type = V4L2_CTRL_TYPE_BOOLEAN,
  258. .name = "auto white balance",
  259. .minimum = 0x00,
  260. .maximum = 0x01,
  261. .step = 0x01,
  262. .default_value = 0x01,
  263. .flags = 0,
  264. },
  265. {
  266. .id = V4L2_CID_AUTOGAIN,
  267. .type = V4L2_CTRL_TYPE_INTEGER,
  268. .name = "gain & exposure mode",
  269. .minimum = 0x00,
  270. .maximum = 0x03,
  271. .step = 0x01,
  272. .default_value = 0x00,
  273. .flags = 0,
  274. },
  275. {
  276. .id = V4L2_CID_VFLIP,
  277. .type = V4L2_CTRL_TYPE_BOOLEAN,
  278. .name = "vertical flip",
  279. .minimum = 0x00,
  280. .maximum = 0x01,
  281. .step = 0x01,
  282. .default_value = 0x01,
  283. .flags = 0,
  284. },
  285. {
  286. .id = V4L2_CID_BLACK_LEVEL,
  287. .type = V4L2_CTRL_TYPE_INTEGER,
  288. .name = "black pixel ratio",
  289. .minimum = 0x01,
  290. .maximum = 0x9a,
  291. .step = 0x01,
  292. .default_value = 0x8a,
  293. .flags = 0,
  294. },
  295. {
  296. .id = SN9C102_V4L2_CID_BRIGHT_LEVEL,
  297. .type = V4L2_CTRL_TYPE_INTEGER,
  298. .name = "bright pixel ratio",
  299. .minimum = 0x01,
  300. .maximum = 0x9a,
  301. .step = 0x01,
  302. .default_value = 0x10,
  303. .flags = 0,
  304. },
  305. {
  306. .id = SN9C102_V4L2_CID_BAND_FILTER,
  307. .type = V4L2_CTRL_TYPE_BOOLEAN,
  308. .name = "band filter",
  309. .minimum = 0x00,
  310. .maximum = 0x01,
  311. .step = 0x01,
  312. .default_value = 0x00,
  313. .flags = 0,
  314. },
  315. {
  316. .id = SN9C102_V4L2_CID_GAMMA,
  317. .type = V4L2_CTRL_TYPE_BOOLEAN,
  318. .name = "rgb gamma",
  319. .minimum = 0x00,
  320. .maximum = 0x01,
  321. .step = 0x01,
  322. .default_value = 0x00,
  323. .flags = 0,
  324. },
  325. },
  326. .set_ctrl = &ov7630_set_ctrl,
  327. .cropcap = {
  328. .bounds = {
  329. .left = 0,
  330. .top = 0,
  331. .width = 640,
  332. .height = 480,
  333. },
  334. .defrect = {
  335. .left = 0,
  336. .top = 0,
  337. .width = 640,
  338. .height = 480,
  339. },
  340. },
  341. .set_crop = &ov7630_set_crop,
  342. .pix_format = {
  343. .width = 640,
  344. .height = 480,
  345. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  346. .priv = 8,
  347. },
  348. .set_pix_format = &ov7630_set_pix_format
  349. };
  350. int sn9c102_probe_ov7630(struct sn9c102_device* cam)
  351. {
  352. const struct usb_device_id ov7630_id_table[] = {
  353. { USB_DEVICE(0x0c45, 0x602c), },
  354. { USB_DEVICE(0x0c45, 0x602d), },
  355. { USB_DEVICE(0x0c45, 0x608f), },
  356. { USB_DEVICE(0x0c45, 0x60b0), },
  357. { }
  358. };
  359. int err = 0;
  360. if (!sn9c102_match_id(cam, ov7630_id_table))
  361. return -ENODEV;
  362. err += sn9c102_write_reg(cam, 0x01, 0x01);
  363. err += sn9c102_write_reg(cam, 0x00, 0x01);
  364. err += sn9c102_write_reg(cam, 0x28, 0x17);
  365. if (err)
  366. return -EIO;
  367. err += sn9c102_i2c_try_write(cam, &ov7630, 0x0b, 0);
  368. if (err)
  369. return -ENODEV;
  370. sn9c102_attach_sensor(cam, &ov7630);
  371. return 0;
  372. }