sn9c102_mi0343.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /***************************************************************************
  2. * Plug-in for MI-0343 image sensor connected to the SN9C1xx PC Camera *
  3. * Controllers *
  4. * *
  5. * Copyright (C) 2004-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 mi0343_init(struct sn9c102_device* cam)
  23. {
  24. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  25. int err = 0;
  26. err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
  27. {0x0a, 0x14}, {0x40, 0x01},
  28. {0x20, 0x17}, {0x07, 0x18},
  29. {0xa0, 0x19});
  30. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  31. 0x00, 0x01, 0, 0);
  32. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  33. 0x00, 0x00, 0, 0);
  34. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
  35. 0x01, 0xe1, 0, 0);
  36. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
  37. 0x02, 0x81, 0, 0);
  38. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
  39. 0x00, 0x17, 0, 0);
  40. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
  41. 0x00, 0x11, 0, 0);
  42. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
  43. 0x04, 0x9a, 0, 0);
  44. return err;
  45. }
  46. static int mi0343_get_ctrl(struct sn9c102_device* cam,
  47. struct v4l2_control* ctrl)
  48. {
  49. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  50. u8 data[2];
  51. switch (ctrl->id) {
  52. case V4L2_CID_EXPOSURE:
  53. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2,
  54. data) < 0)
  55. return -EIO;
  56. ctrl->value = data[0];
  57. return 0;
  58. case V4L2_CID_GAIN:
  59. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2,
  60. data) < 0)
  61. return -EIO;
  62. break;
  63. case V4L2_CID_HFLIP:
  64. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
  65. data) < 0)
  66. return -EIO;
  67. ctrl->value = data[1] & 0x20 ? 1 : 0;
  68. return 0;
  69. case V4L2_CID_VFLIP:
  70. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
  71. data) < 0)
  72. return -EIO;
  73. ctrl->value = data[1] & 0x80 ? 1 : 0;
  74. return 0;
  75. case V4L2_CID_RED_BALANCE:
  76. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2,
  77. data) < 0)
  78. return -EIO;
  79. break;
  80. case V4L2_CID_BLUE_BALANCE:
  81. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2,
  82. data) < 0)
  83. return -EIO;
  84. break;
  85. case SN9C102_V4L2_CID_GREEN_BALANCE:
  86. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2,
  87. data) < 0)
  88. return -EIO;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. switch (ctrl->id) {
  94. case V4L2_CID_GAIN:
  95. case V4L2_CID_RED_BALANCE:
  96. case V4L2_CID_BLUE_BALANCE:
  97. case SN9C102_V4L2_CID_GREEN_BALANCE:
  98. ctrl->value = data[1] | (data[0] << 8);
  99. if (ctrl->value >= 0x10 && ctrl->value <= 0x3f)
  100. ctrl->value -= 0x10;
  101. else if (ctrl->value >= 0x60 && ctrl->value <= 0x7f)
  102. ctrl->value -= 0x60;
  103. else if (ctrl->value >= 0xe0 && ctrl->value <= 0xff)
  104. ctrl->value -= 0xe0;
  105. }
  106. return 0;
  107. }
  108. static int mi0343_set_ctrl(struct sn9c102_device* cam,
  109. const struct v4l2_control* ctrl)
  110. {
  111. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  112. u16 reg = 0;
  113. int err = 0;
  114. switch (ctrl->id) {
  115. case V4L2_CID_GAIN:
  116. case V4L2_CID_RED_BALANCE:
  117. case V4L2_CID_BLUE_BALANCE:
  118. case SN9C102_V4L2_CID_GREEN_BALANCE:
  119. if (ctrl->value <= (0x3f-0x10))
  120. reg = 0x10 + ctrl->value;
  121. else if (ctrl->value <= ((0x3f-0x10) + (0x7f-0x60)))
  122. reg = 0x60 + (ctrl->value - (0x3f-0x10));
  123. else
  124. reg = 0xe0 + (ctrl->value - (0x3f-0x10) - (0x7f-0x60));
  125. break;
  126. }
  127. switch (ctrl->id) {
  128. case V4L2_CID_EXPOSURE:
  129. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  130. 0x09, ctrl->value, 0x00,
  131. 0, 0);
  132. break;
  133. case V4L2_CID_GAIN:
  134. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  135. 0x35, reg >> 8, reg & 0xff,
  136. 0, 0);
  137. break;
  138. case V4L2_CID_HFLIP:
  139. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  140. 0x20, ctrl->value ? 0x40:0x00,
  141. ctrl->value ? 0x20:0x00,
  142. 0, 0);
  143. break;
  144. case V4L2_CID_VFLIP:
  145. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  146. 0x20, ctrl->value ? 0x80:0x00,
  147. ctrl->value ? 0x80:0x00,
  148. 0, 0);
  149. break;
  150. case V4L2_CID_RED_BALANCE:
  151. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  152. 0x2d, reg >> 8, reg & 0xff,
  153. 0, 0);
  154. break;
  155. case V4L2_CID_BLUE_BALANCE:
  156. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  157. 0x2c, reg >> 8, reg & 0xff,
  158. 0, 0);
  159. break;
  160. case SN9C102_V4L2_CID_GREEN_BALANCE:
  161. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  162. 0x2b, reg >> 8, reg & 0xff,
  163. 0, 0);
  164. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  165. 0x2e, reg >> 8, reg & 0xff,
  166. 0, 0);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. return err ? -EIO : 0;
  172. }
  173. static int mi0343_set_crop(struct sn9c102_device* cam,
  174. const struct v4l2_rect* rect)
  175. {
  176. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  177. int err = 0;
  178. u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0,
  179. v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
  180. err += sn9c102_write_reg(cam, h_start, 0x12);
  181. err += sn9c102_write_reg(cam, v_start, 0x13);
  182. return err;
  183. }
  184. static int mi0343_set_pix_format(struct sn9c102_device* cam,
  185. const struct v4l2_pix_format* pix)
  186. {
  187. struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
  188. int err = 0;
  189. if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) {
  190. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  191. 0x0a, 0x00, 0x03, 0, 0);
  192. err += sn9c102_write_reg(cam, 0x20, 0x19);
  193. } else {
  194. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  195. 0x0a, 0x00, 0x05, 0, 0);
  196. err += sn9c102_write_reg(cam, 0xa0, 0x19);
  197. }
  198. return err;
  199. }
  200. static const struct sn9c102_sensor mi0343 = {
  201. .name = "MI-0343",
  202. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  203. .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
  204. .frequency = SN9C102_I2C_100KHZ,
  205. .interface = SN9C102_I2C_2WIRES,
  206. .i2c_slave_id = 0x5d,
  207. .init = &mi0343_init,
  208. .qctrl = {
  209. {
  210. .id = V4L2_CID_EXPOSURE,
  211. .type = V4L2_CTRL_TYPE_INTEGER,
  212. .name = "exposure",
  213. .minimum = 0x00,
  214. .maximum = 0x0f,
  215. .step = 0x01,
  216. .default_value = 0x06,
  217. .flags = 0,
  218. },
  219. {
  220. .id = V4L2_CID_GAIN,
  221. .type = V4L2_CTRL_TYPE_INTEGER,
  222. .name = "global gain",
  223. .minimum = 0x00,
  224. .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/
  225. .step = 0x01,
  226. .default_value = 0x00,
  227. .flags = 0,
  228. },
  229. {
  230. .id = V4L2_CID_HFLIP,
  231. .type = V4L2_CTRL_TYPE_BOOLEAN,
  232. .name = "horizontal mirror",
  233. .minimum = 0,
  234. .maximum = 1,
  235. .step = 1,
  236. .default_value = 0,
  237. .flags = 0,
  238. },
  239. {
  240. .id = V4L2_CID_VFLIP,
  241. .type = V4L2_CTRL_TYPE_BOOLEAN,
  242. .name = "vertical mirror",
  243. .minimum = 0,
  244. .maximum = 1,
  245. .step = 1,
  246. .default_value = 0,
  247. .flags = 0,
  248. },
  249. {
  250. .id = V4L2_CID_RED_BALANCE,
  251. .type = V4L2_CTRL_TYPE_INTEGER,
  252. .name = "red balance",
  253. .minimum = 0x00,
  254. .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
  255. .step = 0x01,
  256. .default_value = 0x00,
  257. .flags = 0,
  258. },
  259. {
  260. .id = V4L2_CID_BLUE_BALANCE,
  261. .type = V4L2_CTRL_TYPE_INTEGER,
  262. .name = "blue balance",
  263. .minimum = 0x00,
  264. .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
  265. .step = 0x01,
  266. .default_value = 0x00,
  267. .flags = 0,
  268. },
  269. {
  270. .id = SN9C102_V4L2_CID_GREEN_BALANCE,
  271. .type = V4L2_CTRL_TYPE_INTEGER,
  272. .name = "green balance",
  273. .minimum = 0x00,
  274. .maximum = ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)),
  275. .step = 0x01,
  276. .default_value = 0x00,
  277. .flags = 0,
  278. },
  279. },
  280. .get_ctrl = &mi0343_get_ctrl,
  281. .set_ctrl = &mi0343_set_ctrl,
  282. .cropcap = {
  283. .bounds = {
  284. .left = 0,
  285. .top = 0,
  286. .width = 640,
  287. .height = 480,
  288. },
  289. .defrect = {
  290. .left = 0,
  291. .top = 0,
  292. .width = 640,
  293. .height = 480,
  294. },
  295. },
  296. .set_crop = &mi0343_set_crop,
  297. .pix_format = {
  298. .width = 640,
  299. .height = 480,
  300. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  301. .priv = 8,
  302. },
  303. .set_pix_format = &mi0343_set_pix_format
  304. };
  305. int sn9c102_probe_mi0343(struct sn9c102_device* cam)
  306. {
  307. u8 data[2];
  308. if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
  309. {0x28, 0x17}))
  310. return -EIO;
  311. if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id, 0x00,
  312. 2, data) < 0)
  313. return -EIO;
  314. if (data[1] != 0x42 || data[0] != 0xe3)
  315. return -ENODEV;
  316. sn9c102_attach_sensor(cam, &mi0343);
  317. return 0;
  318. }