sn9c102_mi0343.c 9.5 KB

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