sn9c102_mi0360.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /***************************************************************************
  2. * Plug-in for MI-0360 image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 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. #include "sn9c102_devtable.h"
  23. static int mi0360_init(struct sn9c102_device* cam)
  24. {
  25. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  26. int err = 0;
  27. switch (sn9c102_get_bridge(cam)) {
  28. case BRIDGE_SN9C103:
  29. err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
  30. {0x0a, 0x14}, {0x40, 0x01},
  31. {0x20, 0x17}, {0x07, 0x18},
  32. {0xa0, 0x19}, {0x02, 0x1c},
  33. {0x03, 0x1d}, {0x0f, 0x1e},
  34. {0x0c, 0x1f}, {0x00, 0x20},
  35. {0x10, 0x21}, {0x20, 0x22},
  36. {0x30, 0x23}, {0x40, 0x24},
  37. {0x50, 0x25}, {0x60, 0x26},
  38. {0x70, 0x27}, {0x80, 0x28},
  39. {0x90, 0x29}, {0xa0, 0x2a},
  40. {0xb0, 0x2b}, {0xc0, 0x2c},
  41. {0xd0, 0x2d}, {0xe0, 0x2e},
  42. {0xf0, 0x2f}, {0xff, 0x30});
  43. break;
  44. case BRIDGE_SN9C105:
  45. case BRIDGE_SN9C120:
  46. err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02},
  47. {0x00, 0x03}, {0x1a, 0x04},
  48. {0x50, 0x05}, {0x20, 0x06},
  49. {0x10, 0x07}, {0x03, 0x10},
  50. {0x08, 0x14}, {0xa2, 0x17},
  51. {0x47, 0x18}, {0x00, 0x19},
  52. {0x1d, 0x1a}, {0x10, 0x1b},
  53. {0x02, 0x1c}, {0x03, 0x1d},
  54. {0x0f, 0x1e}, {0x0c, 0x1f},
  55. {0x00, 0x20}, {0x29, 0x21},
  56. {0x40, 0x22}, {0x54, 0x23},
  57. {0x66, 0x24}, {0x76, 0x25},
  58. {0x85, 0x26}, {0x94, 0x27},
  59. {0xa1, 0x28}, {0xae, 0x29},
  60. {0xbb, 0x2a}, {0xc7, 0x2b},
  61. {0xd3, 0x2c}, {0xde, 0x2d},
  62. {0xea, 0x2e}, {0xf4, 0x2f},
  63. {0xff, 0x30}, {0x00, 0x3F},
  64. {0xC7, 0x40}, {0x01, 0x41},
  65. {0x44, 0x42}, {0x00, 0x43},
  66. {0x44, 0x44}, {0x00, 0x45},
  67. {0x44, 0x46}, {0x00, 0x47},
  68. {0xC7, 0x48}, {0x01, 0x49},
  69. {0xC7, 0x4A}, {0x01, 0x4B},
  70. {0xC7, 0x4C}, {0x01, 0x4D},
  71. {0x44, 0x4E}, {0x00, 0x4F},
  72. {0x44, 0x50}, {0x00, 0x51},
  73. {0x44, 0x52}, {0x00, 0x53},
  74. {0xC7, 0x54}, {0x01, 0x55},
  75. {0xC7, 0x56}, {0x01, 0x57},
  76. {0xC7, 0x58}, {0x01, 0x59},
  77. {0x44, 0x5A}, {0x00, 0x5B},
  78. {0x44, 0x5C}, {0x00, 0x5D},
  79. {0x44, 0x5E}, {0x00, 0x5F},
  80. {0xC7, 0x60}, {0x01, 0x61},
  81. {0xC7, 0x62}, {0x01, 0x63},
  82. {0xC7, 0x64}, {0x01, 0x65},
  83. {0x44, 0x66}, {0x00, 0x67},
  84. {0x44, 0x68}, {0x00, 0x69},
  85. {0x44, 0x6A}, {0x00, 0x6B},
  86. {0xC7, 0x6C}, {0x01, 0x6D},
  87. {0xC7, 0x6E}, {0x01, 0x6F},
  88. {0xC7, 0x70}, {0x01, 0x71},
  89. {0x44, 0x72}, {0x00, 0x73},
  90. {0x44, 0x74}, {0x00, 0x75},
  91. {0x44, 0x76}, {0x00, 0x77},
  92. {0xC7, 0x78}, {0x01, 0x79},
  93. {0xC7, 0x7A}, {0x01, 0x7B},
  94. {0xC7, 0x7C}, {0x01, 0x7D},
  95. {0x44, 0x7E}, {0x00, 0x7F},
  96. {0x14, 0x84}, {0x00, 0x85},
  97. {0x27, 0x86}, {0x00, 0x87},
  98. {0x07, 0x88}, {0x00, 0x89},
  99. {0xEC, 0x8A}, {0x0f, 0x8B},
  100. {0xD8, 0x8C}, {0x0f, 0x8D},
  101. {0x3D, 0x8E}, {0x00, 0x8F},
  102. {0x3D, 0x90}, {0x00, 0x91},
  103. {0xCD, 0x92}, {0x0f, 0x93},
  104. {0xf7, 0x94}, {0x0f, 0x95},
  105. {0x0C, 0x96}, {0x00, 0x97},
  106. {0x00, 0x98}, {0x66, 0x99},
  107. {0x05, 0x9A}, {0x00, 0x9B},
  108. {0x04, 0x9C}, {0x00, 0x9D},
  109. {0x08, 0x9E}, {0x00, 0x9F},
  110. {0x2D, 0xC0}, {0x2D, 0xC1},
  111. {0x3A, 0xC2}, {0x05, 0xC3},
  112. {0x04, 0xC4}, {0x3F, 0xC5},
  113. {0x00, 0xC6}, {0x00, 0xC7},
  114. {0x50, 0xC8}, {0x3C, 0xC9},
  115. {0x28, 0xCA}, {0xD8, 0xCB},
  116. {0x14, 0xCC}, {0xEC, 0xCD},
  117. {0x32, 0xCE}, {0xDD, 0xCF},
  118. {0x32, 0xD0}, {0xDD, 0xD1},
  119. {0x6A, 0xD2}, {0x50, 0xD3},
  120. {0x00, 0xD4}, {0x00, 0xD5},
  121. {0x00, 0xD6});
  122. break;
  123. default:
  124. break;
  125. }
  126. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  127. 0x00, 0x01, 0, 0);
  128. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  129. 0x00, 0x00, 0, 0);
  130. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
  131. 0x01, 0xe1, 0, 0);
  132. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
  133. 0x02, 0x81, 0, 0);
  134. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
  135. 0x00, 0x17, 0, 0);
  136. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
  137. 0x00, 0x11, 0, 0);
  138. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
  139. 0x04, 0x9a, 0, 0);
  140. return err;
  141. }
  142. static int mi0360_get_ctrl(struct sn9c102_device* cam,
  143. struct v4l2_control* ctrl)
  144. {
  145. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  146. u8 data[2];
  147. switch (ctrl->id) {
  148. case V4L2_CID_EXPOSURE:
  149. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2,
  150. data) < 0)
  151. return -EIO;
  152. ctrl->value = data[0];
  153. return 0;
  154. case V4L2_CID_GAIN:
  155. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2,
  156. data) < 0)
  157. return -EIO;
  158. ctrl->value = data[1];
  159. return 0;
  160. case V4L2_CID_RED_BALANCE:
  161. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2,
  162. data) < 0)
  163. return -EIO;
  164. ctrl->value = data[1];
  165. return 0;
  166. case V4L2_CID_BLUE_BALANCE:
  167. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2,
  168. data) < 0)
  169. return -EIO;
  170. ctrl->value = data[1];
  171. return 0;
  172. case SN9C102_V4L2_CID_GREEN_BALANCE:
  173. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2,
  174. data) < 0)
  175. return -EIO;
  176. ctrl->value = data[1];
  177. return 0;
  178. case V4L2_CID_HFLIP:
  179. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
  180. data) < 0)
  181. return -EIO;
  182. ctrl->value = data[1] & 0x20 ? 1 : 0;
  183. return 0;
  184. case V4L2_CID_VFLIP:
  185. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
  186. data) < 0)
  187. return -EIO;
  188. ctrl->value = data[1] & 0x80 ? 1 : 0;
  189. return 0;
  190. default:
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. static int mi0360_set_ctrl(struct sn9c102_device* cam,
  196. const struct v4l2_control* ctrl)
  197. {
  198. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  199. int err = 0;
  200. switch (ctrl->id) {
  201. case V4L2_CID_EXPOSURE:
  202. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  203. 0x09, ctrl->value, 0x00,
  204. 0, 0);
  205. break;
  206. case V4L2_CID_GAIN:
  207. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  208. 0x35, 0x03, ctrl->value,
  209. 0, 0);
  210. break;
  211. case V4L2_CID_RED_BALANCE:
  212. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  213. 0x2c, 0x03, ctrl->value,
  214. 0, 0);
  215. break;
  216. case V4L2_CID_BLUE_BALANCE:
  217. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  218. 0x2d, 0x03, ctrl->value,
  219. 0, 0);
  220. break;
  221. case SN9C102_V4L2_CID_GREEN_BALANCE:
  222. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  223. 0x2b, 0x03, ctrl->value,
  224. 0, 0);
  225. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  226. 0x2e, 0x03, ctrl->value,
  227. 0, 0);
  228. break;
  229. case V4L2_CID_HFLIP:
  230. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  231. 0x20, ctrl->value ? 0x40:0x00,
  232. ctrl->value ? 0x20:0x00,
  233. 0, 0);
  234. break;
  235. case V4L2_CID_VFLIP:
  236. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  237. 0x20, ctrl->value ? 0x80:0x00,
  238. ctrl->value ? 0x80:0x00,
  239. 0, 0);
  240. break;
  241. default:
  242. return -EINVAL;
  243. }
  244. return err ? -EIO : 0;
  245. }
  246. static int mi0360_set_crop(struct sn9c102_device* cam,
  247. const struct v4l2_rect* rect)
  248. {
  249. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  250. int err = 0;
  251. u8 h_start = 0, v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
  252. switch (sn9c102_get_bridge(cam)) {
  253. case BRIDGE_SN9C103:
  254. h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0;
  255. break;
  256. case BRIDGE_SN9C105:
  257. case BRIDGE_SN9C120:
  258. h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1;
  259. break;
  260. default:
  261. break;
  262. }
  263. err += sn9c102_write_reg(cam, h_start, 0x12);
  264. err += sn9c102_write_reg(cam, v_start, 0x13);
  265. return err;
  266. }
  267. static int mi0360_set_pix_format(struct sn9c102_device* cam,
  268. const struct v4l2_pix_format* pix)
  269. {
  270. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  271. int err = 0;
  272. if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
  273. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  274. 0x0a, 0x00, 0x05, 0, 0);
  275. err += sn9c102_write_reg(cam, 0x60, 0x19);
  276. if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
  277. sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
  278. err += sn9c102_write_reg(cam, 0xa6, 0x17);
  279. } else {
  280. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  281. 0x0a, 0x00, 0x02, 0, 0);
  282. err += sn9c102_write_reg(cam, 0x20, 0x19);
  283. if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
  284. sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
  285. err += sn9c102_write_reg(cam, 0xa2, 0x17);
  286. }
  287. return err;
  288. }
  289. static const struct sn9c102_sensor mi0360 = {
  290. .name = "MI-0360",
  291. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  292. .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120,
  293. .frequency = SN9C102_I2C_100KHZ,
  294. .interface = SN9C102_I2C_2WIRES,
  295. .i2c_slave_id = 0x5d,
  296. .init = &mi0360_init,
  297. .qctrl = {
  298. {
  299. .id = V4L2_CID_EXPOSURE,
  300. .type = V4L2_CTRL_TYPE_INTEGER,
  301. .name = "exposure",
  302. .minimum = 0x00,
  303. .maximum = 0x0f,
  304. .step = 0x01,
  305. .default_value = 0x05,
  306. .flags = 0,
  307. },
  308. {
  309. .id = V4L2_CID_GAIN,
  310. .type = V4L2_CTRL_TYPE_INTEGER,
  311. .name = "global gain",
  312. .minimum = 0x00,
  313. .maximum = 0x7f,
  314. .step = 0x01,
  315. .default_value = 0x25,
  316. .flags = 0,
  317. },
  318. {
  319. .id = V4L2_CID_HFLIP,
  320. .type = V4L2_CTRL_TYPE_BOOLEAN,
  321. .name = "horizontal mirror",
  322. .minimum = 0,
  323. .maximum = 1,
  324. .step = 1,
  325. .default_value = 0,
  326. .flags = 0,
  327. },
  328. {
  329. .id = V4L2_CID_VFLIP,
  330. .type = V4L2_CTRL_TYPE_BOOLEAN,
  331. .name = "vertical mirror",
  332. .minimum = 0,
  333. .maximum = 1,
  334. .step = 1,
  335. .default_value = 0,
  336. .flags = 0,
  337. },
  338. {
  339. .id = V4L2_CID_BLUE_BALANCE,
  340. .type = V4L2_CTRL_TYPE_INTEGER,
  341. .name = "blue balance",
  342. .minimum = 0x00,
  343. .maximum = 0x7f,
  344. .step = 0x01,
  345. .default_value = 0x0f,
  346. .flags = 0,
  347. },
  348. {
  349. .id = V4L2_CID_RED_BALANCE,
  350. .type = V4L2_CTRL_TYPE_INTEGER,
  351. .name = "red balance",
  352. .minimum = 0x00,
  353. .maximum = 0x7f,
  354. .step = 0x01,
  355. .default_value = 0x32,
  356. .flags = 0,
  357. },
  358. {
  359. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  360. .type = V4L2_CTRL_TYPE_INTEGER,
  361. .name = "green balance",
  362. .minimum = 0x00,
  363. .maximum = 0x7f,
  364. .step = 0x01,
  365. .default_value = 0x25,
  366. .flags = 0,
  367. },
  368. },
  369. .get_ctrl = &mi0360_get_ctrl,
  370. .set_ctrl = &mi0360_set_ctrl,
  371. .cropcap = {
  372. .bounds = {
  373. .left = 0,
  374. .top = 0,
  375. .width = 640,
  376. .height = 480,
  377. },
  378. .defrect = {
  379. .left = 0,
  380. .top = 0,
  381. .width = 640,
  382. .height = 480,
  383. },
  384. },
  385. .set_crop = &mi0360_set_crop,
  386. .pix_format = {
  387. .width = 640,
  388. .height = 480,
  389. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  390. .priv = 8,
  391. },
  392. .set_pix_format = &mi0360_set_pix_format
  393. };
  394. int sn9c102_probe_mi0360(struct sn9c102_device* cam)
  395. {
  396. u8 data[2];
  397. switch (sn9c102_get_bridge(cam)) {
  398. case BRIDGE_SN9C103:
  399. if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
  400. {0x28, 0x17}))
  401. return -EIO;
  402. break;
  403. case BRIDGE_SN9C105:
  404. case BRIDGE_SN9C120:
  405. if (sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1},
  406. {0x01, 0x01}, {0x00, 0x01},
  407. {0x28, 0x17}))
  408. return -EIO;
  409. break;
  410. default:
  411. break;
  412. }
  413. if (sn9c102_i2c_try_raw_read(cam, &mi0360, mi0360.i2c_slave_id, 0x00,
  414. 2, data) < 0)
  415. return -EIO;
  416. if (data[0] != 0x82 || data[1] != 0x43)
  417. return -ENODEV;
  418. sn9c102_attach_sensor(cam, &mi0360);
  419. return 0;
  420. }