sn9c102_ov7630.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /***************************************************************************
  2. * Plug-in for OV7630 image sensor connected to the SN9C10x PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2005 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, 0x8d);
  31. err += sn9c102_i2c_write(cam, 0x11, 0x00);
  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, 0x44);
  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, 0x20);
  44. err += sn9c102_i2c_write(cam, 0x29, 0x30);
  45. err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
  46. err += sn9c102_i2c_write(cam, 0x30, 0x24);
  47. err += sn9c102_i2c_write(cam, 0x32, 0x86);
  48. err += sn9c102_i2c_write(cam, 0x60, 0xa9);
  49. err += sn9c102_i2c_write(cam, 0x61, 0x42);
  50. err += sn9c102_i2c_write(cam, 0x65, 0x00);
  51. err += sn9c102_i2c_write(cam, 0x69, 0x38);
  52. err += sn9c102_i2c_write(cam, 0x6f, 0x88);
  53. err += sn9c102_i2c_write(cam, 0x70, 0x0b);
  54. err += sn9c102_i2c_write(cam, 0x71, 0x00);
  55. err += sn9c102_i2c_write(cam, 0x74, 0x21);
  56. err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
  57. return err;
  58. }
  59. static int ov7630_set_ctrl(struct sn9c102_device* cam,
  60. const struct v4l2_control* ctrl)
  61. {
  62. int err = 0;
  63. switch (ctrl->id) {
  64. case V4L2_CID_EXPOSURE:
  65. err += sn9c102_i2c_write(cam, 0x10, ctrl->value >> 2);
  66. err += sn9c102_i2c_write(cam, 0x76, ctrl->value & 0x03);
  67. break;
  68. case V4L2_CID_RED_BALANCE:
  69. err += sn9c102_i2c_write(cam, 0x02, ctrl->value);
  70. break;
  71. case V4L2_CID_BLUE_BALANCE:
  72. err += sn9c102_i2c_write(cam, 0x03, ctrl->value);
  73. break;
  74. case V4L2_CID_GAIN:
  75. err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
  76. break;
  77. case V4L2_CID_CONTRAST:
  78. err += ctrl->value ? sn9c102_i2c_write(cam, 0x05,
  79. (ctrl->value-1) | 0x20)
  80. : sn9c102_i2c_write(cam, 0x05, 0x00);
  81. break;
  82. case V4L2_CID_BRIGHTNESS:
  83. err += sn9c102_i2c_write(cam, 0x06, ctrl->value);
  84. break;
  85. case V4L2_CID_SATURATION:
  86. err += sn9c102_i2c_write(cam, 0x03, ctrl->value << 4);
  87. break;
  88. case V4L2_CID_HUE:
  89. err += ctrl->value ? sn9c102_i2c_write(cam, 0x04,
  90. (ctrl->value-1) | 0x20)
  91. : sn9c102_i2c_write(cam, 0x04, 0x00);
  92. break;
  93. case V4L2_CID_DO_WHITE_BALANCE:
  94. err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
  95. break;
  96. case V4L2_CID_WHITENESS:
  97. err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
  98. break;
  99. case V4L2_CID_AUTO_WHITE_BALANCE:
  100. err += sn9c102_i2c_write(cam, 0x12, (ctrl->value << 2) | 0x09);
  101. break;
  102. case V4L2_CID_AUTOGAIN:
  103. err += sn9c102_i2c_write(cam, 0x13, ctrl->value);
  104. break;
  105. case V4L2_CID_VFLIP:
  106. err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7));
  107. break;
  108. case V4L2_CID_BLACK_LEVEL:
  109. err += sn9c102_i2c_write(cam, 0x25, ctrl->value);
  110. break;
  111. case SN9C102_V4L2_CID_BRIGHT_LEVEL:
  112. err += sn9c102_i2c_write(cam, 0x24, ctrl->value);
  113. break;
  114. case SN9C102_V4L2_CID_GAMMA:
  115. err += sn9c102_i2c_write(cam, 0x14, (ctrl->value << 2) | 0x80);
  116. break;
  117. case SN9C102_V4L2_CID_BAND_FILTER:
  118. err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2);
  119. break;
  120. default:
  121. return -EINVAL;
  122. }
  123. return err ? -EIO : 0;
  124. }
  125. static int ov7630_set_crop(struct sn9c102_device* cam,
  126. const struct v4l2_rect* rect)
  127. {
  128. struct sn9c102_sensor* s = &ov7630;
  129. int err = 0;
  130. u8 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
  131. err += sn9c102_write_reg(cam, v_start, 0x13);
  132. return err;
  133. }
  134. static int ov7630_set_pix_format(struct sn9c102_device* cam,
  135. const struct v4l2_pix_format* pix)
  136. {
  137. int err = 0;
  138. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  139. err += sn9c102_write_reg(cam, 0x20, 0x19);
  140. else
  141. err += sn9c102_write_reg(cam, 0x50, 0x19);
  142. return err;
  143. }
  144. static struct sn9c102_sensor ov7630 = {
  145. .name = "OV7630",
  146. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  147. .sysfs_ops = SN9C102_I2C_WRITE,
  148. .frequency = SN9C102_I2C_100KHZ,
  149. .interface = SN9C102_I2C_2WIRES,
  150. .i2c_slave_id = 0x21,
  151. .init = &ov7630_init,
  152. .qctrl = {
  153. {
  154. .id = V4L2_CID_GAIN,
  155. .type = V4L2_CTRL_TYPE_INTEGER,
  156. .name = "global gain",
  157. .minimum = 0x00,
  158. .maximum = 0x3f,
  159. .step = 0x01,
  160. .default_value = 0x14,
  161. .flags = 0,
  162. },
  163. {
  164. .id = V4L2_CID_HUE,
  165. .type = V4L2_CTRL_TYPE_INTEGER,
  166. .name = "hue",
  167. .minimum = 0x00,
  168. .maximum = 0x1f+1,
  169. .step = 0x01,
  170. .default_value = 0x00,
  171. .flags = 0,
  172. },
  173. {
  174. .id = V4L2_CID_SATURATION,
  175. .type = V4L2_CTRL_TYPE_INTEGER,
  176. .name = "saturation",
  177. .minimum = 0x00,
  178. .maximum = 0x0f,
  179. .step = 0x01,
  180. .default_value = 0x08,
  181. .flags = 0,
  182. },
  183. {
  184. .id = V4L2_CID_CONTRAST,
  185. .type = V4L2_CTRL_TYPE_INTEGER,
  186. .name = "contrast",
  187. .minimum = 0x00,
  188. .maximum = 0x1f+1,
  189. .step = 0x01,
  190. .default_value = 0x00,
  191. .flags = 0,
  192. },
  193. {
  194. .id = V4L2_CID_EXPOSURE,
  195. .type = V4L2_CTRL_TYPE_INTEGER,
  196. .name = "exposure",
  197. .minimum = 0x000,
  198. .maximum = 0x3ff,
  199. .step = 0x001,
  200. .default_value = 0x83<<2,
  201. .flags = 0,
  202. },
  203. {
  204. .id = V4L2_CID_RED_BALANCE,
  205. .type = V4L2_CTRL_TYPE_INTEGER,
  206. .name = "red balance",
  207. .minimum = 0x00,
  208. .maximum = 0xff,
  209. .step = 0x01,
  210. .default_value = 0x3a,
  211. .flags = 0,
  212. },
  213. {
  214. .id = V4L2_CID_BLUE_BALANCE,
  215. .type = V4L2_CTRL_TYPE_INTEGER,
  216. .name = "blue balance",
  217. .minimum = 0x00,
  218. .maximum = 0xff,
  219. .step = 0x01,
  220. .default_value = 0x77,
  221. .flags = 0,
  222. },
  223. {
  224. .id = V4L2_CID_BRIGHTNESS,
  225. .type = V4L2_CTRL_TYPE_INTEGER,
  226. .name = "brightness",
  227. .minimum = 0x00,
  228. .maximum = 0xff,
  229. .step = 0x01,
  230. .default_value = 0xa0,
  231. .flags = 0,
  232. },
  233. {
  234. .id = V4L2_CID_DO_WHITE_BALANCE,
  235. .type = V4L2_CTRL_TYPE_INTEGER,
  236. .name = "white balance background: blue",
  237. .minimum = 0x00,
  238. .maximum = 0x3f,
  239. .step = 0x01,
  240. .default_value = 0x20,
  241. .flags = 0,
  242. },
  243. {
  244. .id = V4L2_CID_WHITENESS,
  245. .type = V4L2_CTRL_TYPE_INTEGER,
  246. .name = "white balance background: red",
  247. .minimum = 0x00,
  248. .maximum = 0x3f,
  249. .step = 0x01,
  250. .default_value = 0x20,
  251. .flags = 0,
  252. },
  253. {
  254. .id = V4L2_CID_AUTO_WHITE_BALANCE,
  255. .type = V4L2_CTRL_TYPE_BOOLEAN,
  256. .name = "auto white balance",
  257. .minimum = 0x00,
  258. .maximum = 0x01,
  259. .step = 0x01,
  260. .default_value = 0x01,
  261. .flags = 0,
  262. },
  263. {
  264. .id = V4L2_CID_AUTOGAIN,
  265. .type = V4L2_CTRL_TYPE_INTEGER,
  266. .name = "gain & exposure mode",
  267. .minimum = 0x00,
  268. .maximum = 0x03,
  269. .step = 0x01,
  270. .default_value = 0x00,
  271. .flags = 0,
  272. },
  273. {
  274. .id = V4L2_CID_VFLIP,
  275. .type = V4L2_CTRL_TYPE_BOOLEAN,
  276. .name = "vertical flip",
  277. .minimum = 0x00,
  278. .maximum = 0x01,
  279. .step = 0x01,
  280. .default_value = 0x01,
  281. .flags = 0,
  282. },
  283. {
  284. .id = V4L2_CID_BLACK_LEVEL,
  285. .type = V4L2_CTRL_TYPE_INTEGER,
  286. .name = "black pixel ratio",
  287. .minimum = 0x01,
  288. .maximum = 0x9a,
  289. .step = 0x01,
  290. .default_value = 0x8a,
  291. .flags = 0,
  292. },
  293. {
  294. .id = SN9C102_V4L2_CID_BRIGHT_LEVEL,
  295. .type = V4L2_CTRL_TYPE_INTEGER,
  296. .name = "bright pixel ratio",
  297. .minimum = 0x01,
  298. .maximum = 0x9a,
  299. .step = 0x01,
  300. .default_value = 0x10,
  301. .flags = 0,
  302. },
  303. {
  304. .id = SN9C102_V4L2_CID_BAND_FILTER,
  305. .type = V4L2_CTRL_TYPE_BOOLEAN,
  306. .name = "band filter",
  307. .minimum = 0x00,
  308. .maximum = 0x01,
  309. .step = 0x01,
  310. .default_value = 0x00,
  311. .flags = 0,
  312. },
  313. {
  314. .id = SN9C102_V4L2_CID_GAMMA,
  315. .type = V4L2_CTRL_TYPE_BOOLEAN,
  316. .name = "rgb gamma",
  317. .minimum = 0x00,
  318. .maximum = 0x01,
  319. .step = 0x01,
  320. .default_value = 0x00,
  321. .flags = 0,
  322. },
  323. },
  324. .set_ctrl = &ov7630_set_ctrl,
  325. .cropcap = {
  326. .bounds = {
  327. .left = 0,
  328. .top = 0,
  329. .width = 640,
  330. .height = 480,
  331. },
  332. .defrect = {
  333. .left = 0,
  334. .top = 0,
  335. .width = 640,
  336. .height = 480,
  337. },
  338. },
  339. .set_crop = &ov7630_set_crop,
  340. .pix_format = {
  341. .width = 640,
  342. .height = 480,
  343. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  344. .priv = 8,
  345. },
  346. .set_pix_format = &ov7630_set_pix_format
  347. };
  348. int sn9c102_probe_ov7630(struct sn9c102_device* cam)
  349. {
  350. int err = 0;
  351. sn9c102_attach_sensor(cam, &ov7630);
  352. if (le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x608f &&
  353. le16_to_cpu(ov7630.usbdev->descriptor.idProduct) != 0x602c)
  354. return -ENODEV;
  355. err += sn9c102_write_reg(cam, 0x01, 0x01);
  356. err += sn9c102_write_reg(cam, 0x00, 0x01);
  357. err += sn9c102_write_reg(cam, 0x28, 0x17);
  358. if (err)
  359. return -EIO;
  360. err += sn9c102_i2c_write(cam, 0x0b, 0);
  361. if (err)
  362. return -ENODEV;
  363. return 0;
  364. }