sn9c102_mt9v111.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /***************************************************************************
  2. * Plug-in for MT9V111 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 mt9v111_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, {0x44, 0x01}, {0x40, 0x02},
  28. {0x00, 0x03}, {0x1a, 0x04},
  29. {0x1f, 0x05}, {0x20, 0x06},
  30. {0x1f, 0x07}, {0x81, 0x08},
  31. {0x5c, 0x09}, {0x00, 0x0a},
  32. {0x00, 0x0b}, {0x00, 0x0c},
  33. {0x00, 0x0d}, {0x00, 0x0e},
  34. {0x00, 0x0f}, {0x03, 0x10},
  35. {0x00, 0x11}, {0x00, 0x12},
  36. {0x02, 0x13}, {0x14, 0x14},
  37. {0x28, 0x15}, {0x1e, 0x16},
  38. {0xe2, 0x17}, {0x06, 0x18},
  39. {0x00, 0x19}, {0x00, 0x1a},
  40. {0x00, 0x1b}, {0x08, 0x20},
  41. {0x39, 0x21}, {0x51, 0x22},
  42. {0x63, 0x23}, {0x73, 0x24},
  43. {0x82, 0x25}, {0x8f, 0x26},
  44. {0x9b, 0x27}, {0xa7, 0x28},
  45. {0xb1, 0x29}, {0xbc, 0x2a},
  46. {0xc6, 0x2b}, {0xcf, 0x2c},
  47. {0xd8, 0x2d}, {0xe1, 0x2e},
  48. {0xea, 0x2f}, {0xf2, 0x30},
  49. {0x13, 0x84}, {0x00, 0x85},
  50. {0x25, 0x86}, {0x00, 0x87},
  51. {0x07, 0x88}, {0x00, 0x89},
  52. {0xee, 0x8a}, {0x0f, 0x8b},
  53. {0xe5, 0x8c}, {0x0f, 0x8d},
  54. {0x2e, 0x8e}, {0x00, 0x8f},
  55. {0x30, 0x90}, {0x00, 0x91},
  56. {0xd4, 0x92}, {0x0f, 0x93},
  57. {0xfc, 0x94}, {0x0f, 0x95},
  58. {0x14, 0x96}, {0x00, 0x97},
  59. {0x00, 0x98}, {0x60, 0x99},
  60. {0x07, 0x9a}, {0x40, 0x9b},
  61. {0x20, 0x9c}, {0x00, 0x9d},
  62. {0x00, 0x9e}, {0x00, 0x9f},
  63. {0x2d, 0xc0}, {0x2d, 0xc1},
  64. {0x3a, 0xc2}, {0x05, 0xc3},
  65. {0x04, 0xc4}, {0x3f, 0xc5},
  66. {0x00, 0xc6}, {0x00, 0xc7},
  67. {0x50, 0xc8}, {0x3c, 0xc9},
  68. {0x28, 0xca}, {0xd8, 0xcb},
  69. {0x14, 0xcc}, {0xec, 0xcd},
  70. {0x32, 0xce}, {0xdd, 0xcf},
  71. {0x2d, 0xd0}, {0xdd, 0xd1},
  72. {0x6a, 0xd2}, {0x50, 0xd3},
  73. {0x60, 0xd4}, {0x00, 0xd5},
  74. {0x00, 0xd6});
  75. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
  76. 0x00, 0x01, 0, 0);
  77. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  78. 0x00, 0x01, 0, 0);
  79. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
  80. 0x00, 0x00, 0, 0);
  81. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08,
  82. 0x04, 0x80, 0, 0);
  83. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
  84. 0x00, 0x04, 0, 0);
  85. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x08,
  86. 0x00, 0x08, 0, 0);
  87. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x02,
  88. 0x00, 0x16, 0, 0);
  89. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
  90. 0x01, 0xe7, 0, 0);
  91. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
  92. 0x02, 0x87, 0, 0);
  93. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
  94. 0x00, 0x40, 0, 0);
  95. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
  96. 0x00, 0x09, 0, 0);
  97. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x07,
  98. 0x30, 0x02, 0, 0);
  99. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0c,
  100. 0x00, 0x00, 0, 0);
  101. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x12,
  102. 0x00, 0xb0, 0, 0);
  103. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x13,
  104. 0x00, 0x7c, 0, 0);
  105. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x1e,
  106. 0x00, 0x00, 0, 0);
  107. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20,
  108. 0x00, 0x00, 0, 0);
  109. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x20,
  110. 0x00, 0x00, 0, 0);
  111. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x01,
  112. 0x00, 0x04, 0, 0);
  113. return err;
  114. }
  115. static int mt9v111_get_ctrl(struct sn9c102_device *cam,
  116. struct v4l2_control *ctrl)
  117. {
  118. struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
  119. u8 data[2];
  120. int err = 0;
  121. switch (ctrl->id) {
  122. case V4L2_CID_VFLIP:
  123. if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
  124. data) < 0)
  125. return -EIO;
  126. ctrl->value = data[1] & 0x80 ? 1 : 0;
  127. return 0;
  128. default:
  129. return -EINVAL;
  130. }
  131. return err ? -EIO : 0;
  132. }
  133. static int mt9v111_set_ctrl(struct sn9c102_device *cam,
  134. const struct v4l2_control *ctrl)
  135. {
  136. struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
  137. int err = 0;
  138. switch (ctrl->id) {
  139. case V4L2_CID_VFLIP:
  140. err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
  141. 0x20,
  142. ctrl->value ? 0x80 : 0x00,
  143. ctrl->value ? 0x80 : 0x00, 0,
  144. 0);
  145. break;
  146. default:
  147. return -EINVAL;
  148. }
  149. return err ? -EIO : 0;
  150. }
  151. static int mt9v111_set_crop(struct sn9c102_device *cam,
  152. const struct v4l2_rect *rect)
  153. {
  154. struct sn9c102_sensor *s = sn9c102_get_sensor(cam);
  155. int err = 0;
  156. u8 v_start = (u8) (rect->top - s->cropcap.bounds.top) + 2;
  157. err += sn9c102_write_reg(cam, v_start, 0x13);
  158. return err;
  159. }
  160. static int mt9v111_set_pix_format(struct sn9c102_device *cam,
  161. const struct v4l2_pix_format *pix)
  162. {
  163. int err = 0;
  164. if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
  165. err += sn9c102_write_reg(cam, 0xb4, 0x17);
  166. } else {
  167. err += sn9c102_write_reg(cam, 0xe2, 0x17);
  168. }
  169. return err;
  170. }
  171. static const struct sn9c102_sensor mt9v111 = {
  172. .name = "MT9V111",
  173. .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
  174. .supported_bridge = BRIDGE_SN9C105 | BRIDGE_SN9C120,
  175. .frequency = SN9C102_I2C_100KHZ,
  176. .interface = SN9C102_I2C_2WIRES,
  177. .i2c_slave_id = 0x5c,
  178. .init = &mt9v111_init,
  179. .qctrl = {
  180. {
  181. .id = V4L2_CID_VFLIP,
  182. .type = V4L2_CTRL_TYPE_BOOLEAN,
  183. .name = "vertical mirror",
  184. .minimum = 0,
  185. .maximum = 1,
  186. .step = 1,
  187. .default_value = 0,
  188. .flags = 0,
  189. },
  190. },
  191. .get_ctrl = &mt9v111_get_ctrl,
  192. .set_ctrl = &mt9v111_set_ctrl,
  193. .cropcap = {
  194. .bounds = {
  195. .left = 0,
  196. .top = 0,
  197. .width = 640,
  198. .height = 480,
  199. },
  200. .defrect = {
  201. .left = 0,
  202. .top = 0,
  203. .width = 640,
  204. .height = 480,
  205. },
  206. },
  207. .set_crop = &mt9v111_set_crop,
  208. .pix_format = {
  209. .width = 640,
  210. .height = 480,
  211. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  212. .priv = 8,
  213. },
  214. .set_pix_format = &mt9v111_set_pix_format
  215. };
  216. int sn9c102_probe_mt9v111(struct sn9c102_device *cam)
  217. {
  218. u8 data[2];
  219. int err = 0;
  220. err += sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1},
  221. {0x29, 0x01}, {0x42, 0x17},
  222. {0x62, 0x17}, {0x08, 0x01});
  223. err += sn9c102_i2c_try_raw_write(cam, &mt9v111, 4,
  224. mt9v111.i2c_slave_id, 0x01, 0x00,
  225. 0x04, 0, 0);
  226. if (err || sn9c102_i2c_try_raw_read(cam, &mt9v111,
  227. mt9v111.i2c_slave_id, 0x36, 2,
  228. data) < 0)
  229. return -EIO;
  230. if (data[0] != 0x82 || data[1] != 0x3a)
  231. return -ENODEV;
  232. sn9c102_attach_sensor(cam, &mt9v111);
  233. return 0;
  234. }