sn9c102_ov7630.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /***************************************************************************
  2. * Plug-in for OV7630 image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2006-2007 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 int ov7630_init(struct sn9c102_device* cam)
  23. {
  24. int err = 0;
  25. switch (sn9c102_get_bridge(cam)) {
  26. case BRIDGE_SN9C101:
  27. case BRIDGE_SN9C102:
  28. err = sn9c102_write_const_regs(cam, {0x00, 0x14},
  29. {0x60, 0x17}, {0x0f, 0x18},
  30. {0x50, 0x19});
  31. err += sn9c102_i2c_write(cam, 0x12, 0x8d);
  32. err += sn9c102_i2c_write(cam, 0x12, 0x0d);
  33. err += sn9c102_i2c_write(cam, 0x11, 0x00);
  34. err += sn9c102_i2c_write(cam, 0x15, 0x35);
  35. err += sn9c102_i2c_write(cam, 0x16, 0x03);
  36. err += sn9c102_i2c_write(cam, 0x17, 0x1c);
  37. err += sn9c102_i2c_write(cam, 0x18, 0xbd);
  38. err += sn9c102_i2c_write(cam, 0x19, 0x06);
  39. err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
  40. err += sn9c102_i2c_write(cam, 0x1b, 0x04);
  41. err += sn9c102_i2c_write(cam, 0x20, 0x44);
  42. err += sn9c102_i2c_write(cam, 0x23, 0xee);
  43. err += sn9c102_i2c_write(cam, 0x26, 0xa0);
  44. err += sn9c102_i2c_write(cam, 0x27, 0x9a);
  45. err += sn9c102_i2c_write(cam, 0x28, 0x20);
  46. err += sn9c102_i2c_write(cam, 0x29, 0x30);
  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. break;
  60. case BRIDGE_SN9C103:
  61. err = sn9c102_write_const_regs(cam, {0x00, 0x02}, {0x00, 0x03},
  62. {0x1a, 0x04}, {0x20, 0x05},
  63. {0x20, 0x06}, {0x20, 0x07},
  64. {0x03, 0x10}, {0x0a, 0x14},
  65. {0x60, 0x17}, {0x0f, 0x18},
  66. {0x50, 0x19}, {0x1d, 0x1a},
  67. {0x10, 0x1b}, {0x02, 0x1c},
  68. {0x03, 0x1d}, {0x0f, 0x1e},
  69. {0x0c, 0x1f}, {0x00, 0x20},
  70. {0x10, 0x21}, {0x20, 0x22},
  71. {0x30, 0x23}, {0x40, 0x24},
  72. {0x50, 0x25}, {0x60, 0x26},
  73. {0x70, 0x27}, {0x80, 0x28},
  74. {0x90, 0x29}, {0xa0, 0x2a},
  75. {0xb0, 0x2b}, {0xc0, 0x2c},
  76. {0xd0, 0x2d}, {0xe0, 0x2e},
  77. {0xf0, 0x2f}, {0xff, 0x30});
  78. err += sn9c102_i2c_write(cam, 0x12, 0x8d);
  79. err += sn9c102_i2c_write(cam, 0x12, 0x0d);
  80. err += sn9c102_i2c_write(cam, 0x15, 0x34);
  81. err += sn9c102_i2c_write(cam, 0x11, 0x01);
  82. err += sn9c102_i2c_write(cam, 0x1b, 0x04);
  83. err += sn9c102_i2c_write(cam, 0x20, 0x44);
  84. err += sn9c102_i2c_write(cam, 0x23, 0xee);
  85. err += sn9c102_i2c_write(cam, 0x26, 0xa0);
  86. err += sn9c102_i2c_write(cam, 0x27, 0x9a);
  87. err += sn9c102_i2c_write(cam, 0x28, 0x20);
  88. err += sn9c102_i2c_write(cam, 0x29, 0x30);
  89. err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
  90. err += sn9c102_i2c_write(cam, 0x30, 0x24);
  91. err += sn9c102_i2c_write(cam, 0x32, 0x86);
  92. err += sn9c102_i2c_write(cam, 0x60, 0xa9);
  93. err += sn9c102_i2c_write(cam, 0x61, 0x42);
  94. err += sn9c102_i2c_write(cam, 0x65, 0x00);
  95. err += sn9c102_i2c_write(cam, 0x69, 0x38);
  96. err += sn9c102_i2c_write(cam, 0x6f, 0x88);
  97. err += sn9c102_i2c_write(cam, 0x70, 0x0b);
  98. err += sn9c102_i2c_write(cam, 0x71, 0x00);
  99. err += sn9c102_i2c_write(cam, 0x74, 0x21);
  100. err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
  101. break;
  102. default:
  103. break;
  104. }
  105. return err;
  106. }
  107. static int ov7630_get_ctrl(struct sn9c102_device* cam,
  108. struct v4l2_control* ctrl)
  109. {
  110. int err = 0;
  111. switch (ctrl->id) {
  112. case V4L2_CID_EXPOSURE:
  113. if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0)
  114. return -EIO;
  115. break;
  116. case V4L2_CID_RED_BALANCE:
  117. ctrl->value = sn9c102_pread_reg(cam, 0x07);
  118. break;
  119. case V4L2_CID_BLUE_BALANCE:
  120. ctrl->value = sn9c102_pread_reg(cam, 0x06);
  121. break;
  122. case SN9C102_V4L2_CID_GREEN_BALANCE:
  123. ctrl->value = sn9c102_pread_reg(cam, 0x05);
  124. break;
  125. case V4L2_CID_GAIN:
  126. if ((ctrl->value = sn9c102_i2c_read(cam, 0x00)) < 0)
  127. return -EIO;
  128. ctrl->value &= 0x3f;
  129. break;
  130. case V4L2_CID_DO_WHITE_BALANCE:
  131. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
  132. return -EIO;
  133. ctrl->value &= 0x3f;
  134. break;
  135. case V4L2_CID_WHITENESS:
  136. if ((ctrl->value = sn9c102_i2c_read(cam, 0x0d)) < 0)
  137. return -EIO;
  138. ctrl->value &= 0x3f;
  139. break;
  140. case V4L2_CID_AUTOGAIN:
  141. if ((ctrl->value = sn9c102_i2c_read(cam, 0x13)) < 0)
  142. return -EIO;
  143. ctrl->value &= 0x01;
  144. break;
  145. case V4L2_CID_VFLIP:
  146. if ((ctrl->value = sn9c102_i2c_read(cam, 0x75)) < 0)
  147. return -EIO;
  148. ctrl->value = (ctrl->value & 0x80) ? 1 : 0;
  149. break;
  150. case SN9C102_V4L2_CID_GAMMA:
  151. if ((ctrl->value = sn9c102_i2c_read(cam, 0x14)) < 0)
  152. return -EIO;
  153. ctrl->value = (ctrl->value & 0x02) ? 1 : 0;
  154. break;
  155. case SN9C102_V4L2_CID_BAND_FILTER:
  156. if ((ctrl->value = sn9c102_i2c_read(cam, 0x2d)) < 0)
  157. return -EIO;
  158. ctrl->value = (ctrl->value & 0x02) ? 1 : 0;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. return err ? -EIO : 0;
  164. }
  165. static int ov7630_set_ctrl(struct sn9c102_device* cam,
  166. const struct v4l2_control* ctrl)
  167. {
  168. int err = 0;
  169. switch (ctrl->id) {
  170. case V4L2_CID_EXPOSURE:
  171. err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
  172. break;
  173. case V4L2_CID_RED_BALANCE:
  174. err += sn9c102_write_reg(cam, ctrl->value, 0x07);
  175. break;
  176. case V4L2_CID_BLUE_BALANCE:
  177. err += sn9c102_write_reg(cam, ctrl->value, 0x06);
  178. break;
  179. case SN9C102_V4L2_CID_GREEN_BALANCE:
  180. err += sn9c102_write_reg(cam, ctrl->value, 0x05);
  181. break;
  182. case V4L2_CID_GAIN:
  183. err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
  184. break;
  185. case V4L2_CID_DO_WHITE_BALANCE:
  186. err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
  187. break;
  188. case V4L2_CID_WHITENESS:
  189. err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
  190. break;
  191. case V4L2_CID_AUTOGAIN:
  192. err += sn9c102_i2c_write(cam, 0x13, ctrl->value |
  193. (ctrl->value << 1));
  194. break;
  195. case V4L2_CID_VFLIP:
  196. err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7));
  197. break;
  198. case SN9C102_V4L2_CID_GAMMA:
  199. err += sn9c102_i2c_write(cam, 0x14, ctrl->value << 2);
  200. break;
  201. case SN9C102_V4L2_CID_BAND_FILTER:
  202. err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2);
  203. break;
  204. default:
  205. return -EINVAL;
  206. }
  207. return err ? -EIO : 0;
  208. }
  209. static int ov7630_set_crop(struct sn9c102_device* cam,
  210. const struct v4l2_rect* rect)
  211. {
  212. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  213. int err = 0;
  214. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1,
  215. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
  216. err += sn9c102_write_reg(cam, h_start, 0x12);
  217. err += sn9c102_write_reg(cam, v_start, 0x13);
  218. return err;
  219. }
  220. static int ov7630_set_pix_format(struct sn9c102_device* cam,
  221. const struct v4l2_pix_format* pix)
  222. {
  223. int err = 0;
  224. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
  225. err += sn9c102_write_reg(cam, 0x20, 0x19);
  226. else
  227. err += sn9c102_write_reg(cam, 0x50, 0x19);
  228. return err;
  229. }
  230. static struct sn9c102_sensor ov7630 = {
  231. .name = "OV7630",
  232. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  233. .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103,
  234. .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
  235. .frequency = SN9C102_I2C_100KHZ,
  236. .interface = SN9C102_I2C_2WIRES,
  237. .i2c_slave_id = 0x21,
  238. .init = &ov7630_init,
  239. .qctrl = {
  240. {
  241. .id = V4L2_CID_GAIN,
  242. .type = V4L2_CTRL_TYPE_INTEGER,
  243. .name = "global gain",
  244. .minimum = 0x00,
  245. .maximum = 0x3f,
  246. .step = 0x01,
  247. .default_value = 0x14,
  248. .flags = 0,
  249. },
  250. {
  251. .id = V4L2_CID_EXPOSURE,
  252. .type = V4L2_CTRL_TYPE_INTEGER,
  253. .name = "exposure",
  254. .minimum = 0x00,
  255. .maximum = 0xff,
  256. .step = 0x01,
  257. .default_value = 0x60,
  258. .flags = 0,
  259. },
  260. {
  261. .id = V4L2_CID_WHITENESS,
  262. .type = V4L2_CTRL_TYPE_INTEGER,
  263. .name = "white balance background: red",
  264. .minimum = 0x00,
  265. .maximum = 0x3f,
  266. .step = 0x01,
  267. .default_value = 0x20,
  268. .flags = 0,
  269. },
  270. {
  271. .id = V4L2_CID_DO_WHITE_BALANCE,
  272. .type = V4L2_CTRL_TYPE_INTEGER,
  273. .name = "white balance background: blue",
  274. .minimum = 0x00,
  275. .maximum = 0x3f,
  276. .step = 0x01,
  277. .default_value = 0x20,
  278. .flags = 0,
  279. },
  280. {
  281. .id = V4L2_CID_RED_BALANCE,
  282. .type = V4L2_CTRL_TYPE_INTEGER,
  283. .name = "red balance",
  284. .minimum = 0x00,
  285. .maximum = 0x7f,
  286. .step = 0x01,
  287. .default_value = 0x20,
  288. .flags = 0,
  289. },
  290. {
  291. .id = V4L2_CID_BLUE_BALANCE,
  292. .type = V4L2_CTRL_TYPE_INTEGER,
  293. .name = "blue balance",
  294. .minimum = 0x00,
  295. .maximum = 0x7f,
  296. .step = 0x01,
  297. .default_value = 0x20,
  298. .flags = 0,
  299. },
  300. {
  301. .id = V4L2_CID_AUTOGAIN,
  302. .type = V4L2_CTRL_TYPE_BOOLEAN,
  303. .name = "auto adjust",
  304. .minimum = 0x00,
  305. .maximum = 0x01,
  306. .step = 0x01,
  307. .default_value = 0x00,
  308. .flags = 0,
  309. },
  310. {
  311. .id = V4L2_CID_VFLIP,
  312. .type = V4L2_CTRL_TYPE_BOOLEAN,
  313. .name = "vertical flip",
  314. .minimum = 0x00,
  315. .maximum = 0x01,
  316. .step = 0x01,
  317. .default_value = 0x01,
  318. .flags = 0,
  319. },
  320. {
  321. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  322. .type = V4L2_CTRL_TYPE_INTEGER,
  323. .name = "green balance",
  324. .minimum = 0x00,
  325. .maximum = 0x7f,
  326. .step = 0x01,
  327. .default_value = 0x20,
  328. .flags = 0,
  329. },
  330. {
  331. .id = SN9C102_V4L2_CID_BAND_FILTER,
  332. .type = V4L2_CTRL_TYPE_BOOLEAN,
  333. .name = "band filter",
  334. .minimum = 0x00,
  335. .maximum = 0x01,
  336. .step = 0x01,
  337. .default_value = 0x00,
  338. .flags = 0,
  339. },
  340. {
  341. .id = SN9C102_V4L2_CID_GAMMA,
  342. .type = V4L2_CTRL_TYPE_BOOLEAN,
  343. .name = "rgb gamma",
  344. .minimum = 0x00,
  345. .maximum = 0x01,
  346. .step = 0x01,
  347. .default_value = 0x00,
  348. .flags = 0,
  349. },
  350. },
  351. .get_ctrl = &ov7630_get_ctrl,
  352. .set_ctrl = &ov7630_set_ctrl,
  353. .cropcap = {
  354. .bounds = {
  355. .left = 0,
  356. .top = 0,
  357. .width = 640,
  358. .height = 480,
  359. },
  360. .defrect = {
  361. .left = 0,
  362. .top = 0,
  363. .width = 640,
  364. .height = 480,
  365. },
  366. },
  367. .set_crop = &ov7630_set_crop,
  368. .pix_format = {
  369. .width = 640,
  370. .height = 480,
  371. .pixelformat = V4L2_PIX_FMT_SN9C10X,
  372. .priv = 8,
  373. },
  374. .set_pix_format = &ov7630_set_pix_format
  375. };
  376. int sn9c102_probe_ov7630(struct sn9c102_device* cam)
  377. {
  378. int pid, ver, err = 0;
  379. switch (sn9c102_get_bridge(cam)) {
  380. case BRIDGE_SN9C101:
  381. case BRIDGE_SN9C102:
  382. err = sn9c102_write_const_regs(cam, {0x01, 0x01},
  383. {0x00, 0x01}, {0x28, 0x17});
  384. break;
  385. case BRIDGE_SN9C103: /* do _not_ change anything! */
  386. err = sn9c102_write_const_regs(cam, {0x09, 0x01},
  387. {0x42, 0x01}, {0x28, 0x17},
  388. {0x44, 0x02});
  389. pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a);
  390. if (err || pid < 0) { /* try a different initialization */
  391. err = sn9c102_write_reg(cam, 0x01, 0x01);
  392. err += sn9c102_write_reg(cam, 0x00, 0x01);
  393. }
  394. break;
  395. default:
  396. break;
  397. }
  398. pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a);
  399. ver = sn9c102_i2c_try_read(cam, &ov7630, 0x0b);
  400. if (err || pid < 0 || ver < 0)
  401. return -EIO;
  402. if (pid != 0x76 || ver != 0x31)
  403. return -ENODEV;
  404. sn9c102_attach_sensor(cam, &ov7630);
  405. return 0;
  406. }