sn9c102_mt9v111.c 8.1 KB

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