sn9c102_ov7630.c 12 KB

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