m5602_mt9m111.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Driver for the mt9m111 sensor
  3. *
  4. * Copyright (C) 2008 Erik Andrén
  5. * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #include "m5602_mt9m111.h"
  19. static struct v4l2_pix_format mt9m111_modes[] = {
  20. {
  21. 640,
  22. 480,
  23. V4L2_PIX_FMT_SBGGR8,
  24. V4L2_FIELD_NONE,
  25. .sizeimage = 640 * 480,
  26. .bytesperline = 640,
  27. .colorspace = V4L2_COLORSPACE_SRGB,
  28. .priv = 0
  29. }
  30. };
  31. const static struct ctrl mt9m111_ctrls[] = {
  32. {
  33. {
  34. .id = V4L2_CID_VFLIP,
  35. .type = V4L2_CTRL_TYPE_BOOLEAN,
  36. .name = "vertical flip",
  37. .minimum = 0,
  38. .maximum = 1,
  39. .step = 1,
  40. .default_value = 0
  41. },
  42. .set = mt9m111_set_vflip,
  43. .get = mt9m111_get_vflip
  44. }, {
  45. {
  46. .id = V4L2_CID_HFLIP,
  47. .type = V4L2_CTRL_TYPE_BOOLEAN,
  48. .name = "horizontal flip",
  49. .minimum = 0,
  50. .maximum = 1,
  51. .step = 1,
  52. .default_value = 0
  53. },
  54. .set = mt9m111_set_hflip,
  55. .get = mt9m111_get_hflip
  56. }, {
  57. {
  58. .id = V4L2_CID_GAIN,
  59. .type = V4L2_CTRL_TYPE_INTEGER,
  60. .name = "gain",
  61. .minimum = 0,
  62. .maximum = (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2,
  63. .step = 1,
  64. .default_value = DEFAULT_GAIN,
  65. .flags = V4L2_CTRL_FLAG_SLIDER
  66. },
  67. .set = mt9m111_set_gain,
  68. .get = mt9m111_get_gain
  69. }
  70. };
  71. static void mt9m111_dump_registers(struct sd *sd);
  72. int mt9m111_probe(struct sd *sd)
  73. {
  74. u8 data[2] = {0x00, 0x00};
  75. int i;
  76. if (force_sensor) {
  77. if (force_sensor == MT9M111_SENSOR) {
  78. info("Forcing a %s sensor", mt9m111.name);
  79. goto sensor_found;
  80. }
  81. /* If we want to force another sensor, don't try to probe this
  82. * one */
  83. return -ENODEV;
  84. }
  85. info("Probing for a mt9m111 sensor");
  86. /* Do the preinit */
  87. for (i = 0; i < ARRAY_SIZE(preinit_mt9m111); i++) {
  88. if (preinit_mt9m111[i][0] == BRIDGE) {
  89. m5602_write_bridge(sd,
  90. preinit_mt9m111[i][1],
  91. preinit_mt9m111[i][2]);
  92. } else {
  93. data[0] = preinit_mt9m111[i][2];
  94. data[1] = preinit_mt9m111[i][3];
  95. m5602_write_sensor(sd,
  96. preinit_mt9m111[i][1], data, 2);
  97. }
  98. }
  99. if (m5602_read_sensor(sd, MT9M111_SC_CHIPVER, data, 2))
  100. return -ENODEV;
  101. if ((data[0] == 0x14) && (data[1] == 0x3a)) {
  102. info("Detected a mt9m111 sensor");
  103. goto sensor_found;
  104. }
  105. return -ENODEV;
  106. sensor_found:
  107. sd->gspca_dev.cam.cam_mode = mt9m111_modes;
  108. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(mt9m111_modes);
  109. sd->desc->ctrls = mt9m111_ctrls;
  110. sd->desc->nctrls = ARRAY_SIZE(mt9m111_ctrls);
  111. return 0;
  112. }
  113. int mt9m111_init(struct sd *sd)
  114. {
  115. int i, err = 0;
  116. /* Init the sensor */
  117. for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) {
  118. u8 data[2];
  119. if (init_mt9m111[i][0] == BRIDGE) {
  120. err = m5602_write_bridge(sd,
  121. init_mt9m111[i][1],
  122. init_mt9m111[i][2]);
  123. } else {
  124. data[0] = init_mt9m111[i][2];
  125. data[1] = init_mt9m111[i][3];
  126. err = m5602_write_sensor(sd,
  127. init_mt9m111[i][1], data, 2);
  128. }
  129. }
  130. if (dump_sensor)
  131. mt9m111_dump_registers(sd);
  132. return (err < 0) ? err : 0;
  133. }
  134. int mt9m111_power_down(struct sd *sd)
  135. {
  136. return 0;
  137. }
  138. int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  139. {
  140. int err;
  141. u8 data[2] = {0x00, 0x00};
  142. struct sd *sd = (struct sd *) gspca_dev;
  143. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  144. data, 2);
  145. *val = data[0] & MT9M111_RMB_MIRROR_ROWS;
  146. PDEBUG(D_V4L2, "Read vertical flip %d", *val);
  147. return err;
  148. }
  149. int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  150. {
  151. int err;
  152. u8 data[2] = {0x00, 0x00};
  153. struct sd *sd = (struct sd *) gspca_dev;
  154. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  155. /* Set the correct page map */
  156. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  157. if (err < 0)
  158. return err;
  159. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
  160. if (err < 0)
  161. return err;
  162. data[0] = (data[0] & 0xfe) | val;
  163. err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  164. data, 2);
  165. return err;
  166. }
  167. int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  168. {
  169. int err;
  170. u8 data[2] = {0x00, 0x00};
  171. struct sd *sd = (struct sd *) gspca_dev;
  172. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  173. data, 2);
  174. *val = data[0] & MT9M111_RMB_MIRROR_COLS;
  175. PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
  176. return err;
  177. }
  178. int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  179. {
  180. int err;
  181. u8 data[2] = {0x00, 0x00};
  182. struct sd *sd = (struct sd *) gspca_dev;
  183. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  184. /* Set the correct page map */
  185. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  186. if (err < 0)
  187. return err;
  188. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
  189. if (err < 0)
  190. return err;
  191. data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02);
  192. err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  193. data, 2);
  194. return err;
  195. }
  196. int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  197. {
  198. int err, tmp;
  199. u8 data[2] = {0x00, 0x00};
  200. struct sd *sd = (struct sd *) gspca_dev;
  201. err = m5602_read_sensor(sd, MT9M111_SC_GLOBAL_GAIN, data, 2);
  202. tmp = ((data[1] << 8) | data[0]);
  203. *val = ((tmp & (1 << 10)) * 2) |
  204. ((tmp & (1 << 9)) * 2) |
  205. ((tmp & (1 << 8)) * 2) |
  206. (tmp & 0x7f);
  207. PDEBUG(D_V4L2, "Read gain %d", *val);
  208. return err;
  209. }
  210. int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  211. {
  212. int err, tmp;
  213. u8 data[2] = {0x00, 0x00};
  214. struct sd *sd = (struct sd *) gspca_dev;
  215. /* Set the correct page map */
  216. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  217. if (err < 0)
  218. return err;
  219. if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2)
  220. return -EINVAL;
  221. if ((val >= INITIAL_MAX_GAIN * 2 * 2) &&
  222. (val < (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2))
  223. tmp = (1 << 10) | (val << 9) |
  224. (val << 8) | (val / 8);
  225. else if ((val >= INITIAL_MAX_GAIN * 2) &&
  226. (val < INITIAL_MAX_GAIN * 2 * 2))
  227. tmp = (1 << 9) | (1 << 8) | (val / 4);
  228. else if ((val >= INITIAL_MAX_GAIN) &&
  229. (val < INITIAL_MAX_GAIN * 2))
  230. tmp = (1 << 8) | (val / 2);
  231. else
  232. tmp = val;
  233. data[1] = (tmp & 0xff00) >> 8;
  234. data[0] = (tmp & 0xff);
  235. PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp,
  236. data[1], data[0]);
  237. err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN,
  238. data, 2);
  239. return err;
  240. }
  241. static void mt9m111_dump_registers(struct sd *sd)
  242. {
  243. u8 address, value[2] = {0x00, 0x00};
  244. info("Dumping the mt9m111 register state");
  245. info("Dumping the mt9m111 sensor core registers");
  246. value[1] = MT9M111_SENSOR_CORE;
  247. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  248. for (address = 0; address < 0xff; address++) {
  249. m5602_read_sensor(sd, address, value, 2);
  250. info("register 0x%x contains 0x%x%x",
  251. address, value[0], value[1]);
  252. }
  253. info("Dumping the mt9m111 color pipeline registers");
  254. value[1] = MT9M111_COLORPIPE;
  255. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  256. for (address = 0; address < 0xff; address++) {
  257. m5602_read_sensor(sd, address, value, 2);
  258. info("register 0x%x contains 0x%x%x",
  259. address, value[0], value[1]);
  260. }
  261. info("Dumping the mt9m111 camera control registers");
  262. value[1] = MT9M111_CAMERA_CONTROL;
  263. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  264. for (address = 0; address < 0xff; address++) {
  265. m5602_read_sensor(sd, address, value, 2);
  266. info("register 0x%x contains 0x%x%x",
  267. address, value[0], value[1]);
  268. }
  269. info("mt9m111 register state dump complete");
  270. }