m5602_mt9m111.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "m5602_mt9m111.h"
  20. static int mt9m111_s_ctrl(struct v4l2_ctrl *ctrl);
  21. static void mt9m111_dump_registers(struct sd *sd);
  22. static struct v4l2_pix_format mt9m111_modes[] = {
  23. {
  24. 640,
  25. 480,
  26. V4L2_PIX_FMT_SBGGR8,
  27. V4L2_FIELD_NONE,
  28. .sizeimage = 640 * 480,
  29. .bytesperline = 640,
  30. .colorspace = V4L2_COLORSPACE_SRGB,
  31. .priv = 0
  32. }
  33. };
  34. static const struct v4l2_ctrl_ops mt9m111_ctrl_ops = {
  35. .s_ctrl = mt9m111_s_ctrl,
  36. };
  37. static const struct v4l2_ctrl_config mt9m111_greenbal_cfg = {
  38. .ops = &mt9m111_ctrl_ops,
  39. .id = M5602_V4L2_CID_GREEN_BALANCE,
  40. .name = "Green Balance",
  41. .type = V4L2_CTRL_TYPE_INTEGER,
  42. .min = 0,
  43. .max = 0x7ff,
  44. .step = 1,
  45. .def = MT9M111_GREEN_GAIN_DEFAULT,
  46. .flags = V4L2_CTRL_FLAG_SLIDER,
  47. };
  48. int mt9m111_probe(struct sd *sd)
  49. {
  50. u8 data[2] = {0x00, 0x00};
  51. int i;
  52. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  53. if (force_sensor) {
  54. if (force_sensor == MT9M111_SENSOR) {
  55. pr_info("Forcing a %s sensor\n", mt9m111.name);
  56. goto sensor_found;
  57. }
  58. /* If we want to force another sensor, don't try to probe this
  59. * one */
  60. return -ENODEV;
  61. }
  62. PDEBUG(D_PROBE, "Probing for a mt9m111 sensor");
  63. /* Do the preinit */
  64. for (i = 0; i < ARRAY_SIZE(preinit_mt9m111); i++) {
  65. if (preinit_mt9m111[i][0] == BRIDGE) {
  66. m5602_write_bridge(sd,
  67. preinit_mt9m111[i][1],
  68. preinit_mt9m111[i][2]);
  69. } else {
  70. data[0] = preinit_mt9m111[i][2];
  71. data[1] = preinit_mt9m111[i][3];
  72. m5602_write_sensor(sd,
  73. preinit_mt9m111[i][1], data, 2);
  74. }
  75. }
  76. if (m5602_read_sensor(sd, MT9M111_SC_CHIPVER, data, 2))
  77. return -ENODEV;
  78. if ((data[0] == 0x14) && (data[1] == 0x3a)) {
  79. pr_info("Detected a mt9m111 sensor\n");
  80. goto sensor_found;
  81. }
  82. return -ENODEV;
  83. sensor_found:
  84. sd->gspca_dev.cam.cam_mode = mt9m111_modes;
  85. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(mt9m111_modes);
  86. return 0;
  87. }
  88. int mt9m111_init(struct sd *sd)
  89. {
  90. int i, err = 0;
  91. /* Init the sensor */
  92. for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) {
  93. u8 data[2];
  94. if (init_mt9m111[i][0] == BRIDGE) {
  95. err = m5602_write_bridge(sd,
  96. init_mt9m111[i][1],
  97. init_mt9m111[i][2]);
  98. } else {
  99. data[0] = init_mt9m111[i][2];
  100. data[1] = init_mt9m111[i][3];
  101. err = m5602_write_sensor(sd,
  102. init_mt9m111[i][1], data, 2);
  103. }
  104. }
  105. if (dump_sensor)
  106. mt9m111_dump_registers(sd);
  107. return 0;
  108. }
  109. int mt9m111_init_controls(struct sd *sd)
  110. {
  111. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  112. sd->gspca_dev.vdev.ctrl_handler = hdl;
  113. v4l2_ctrl_handler_init(hdl, 7);
  114. sd->auto_white_bal = v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops,
  115. V4L2_CID_AUTO_WHITE_BALANCE,
  116. 0, 1, 1, 0);
  117. sd->green_bal = v4l2_ctrl_new_custom(hdl, &mt9m111_greenbal_cfg, NULL);
  118. sd->red_bal = v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops,
  119. V4L2_CID_RED_BALANCE, 0, 0x7ff, 1,
  120. MT9M111_RED_GAIN_DEFAULT);
  121. sd->blue_bal = v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops,
  122. V4L2_CID_BLUE_BALANCE, 0, 0x7ff, 1,
  123. MT9M111_BLUE_GAIN_DEFAULT);
  124. v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops, V4L2_CID_GAIN, 0,
  125. (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2, 1,
  126. MT9M111_DEFAULT_GAIN);
  127. sd->hflip = v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops, V4L2_CID_HFLIP,
  128. 0, 1, 1, 0);
  129. sd->vflip = v4l2_ctrl_new_std(hdl, &mt9m111_ctrl_ops, V4L2_CID_VFLIP,
  130. 0, 1, 1, 0);
  131. if (hdl->error) {
  132. pr_err("Could not initialize controls\n");
  133. return hdl->error;
  134. }
  135. v4l2_ctrl_auto_cluster(4, &sd->auto_white_bal, 0, false);
  136. v4l2_ctrl_cluster(2, &sd->hflip);
  137. return 0;
  138. }
  139. int mt9m111_start(struct sd *sd)
  140. {
  141. int i, err = 0;
  142. u8 data[2];
  143. struct cam *cam = &sd->gspca_dev.cam;
  144. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  145. int width = cam->cam_mode[sd->gspca_dev.curr_mode].width - 1;
  146. int height = cam->cam_mode[sd->gspca_dev.curr_mode].height;
  147. for (i = 0; i < ARRAY_SIZE(start_mt9m111) && !err; i++) {
  148. if (start_mt9m111[i][0] == BRIDGE) {
  149. err = m5602_write_bridge(sd,
  150. start_mt9m111[i][1],
  151. start_mt9m111[i][2]);
  152. } else {
  153. data[0] = start_mt9m111[i][2];
  154. data[1] = start_mt9m111[i][3];
  155. err = m5602_write_sensor(sd,
  156. start_mt9m111[i][1], data, 2);
  157. }
  158. }
  159. if (err < 0)
  160. return err;
  161. err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height >> 8) & 0xff);
  162. if (err < 0)
  163. return err;
  164. err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height & 0xff));
  165. if (err < 0)
  166. return err;
  167. for (i = 0; i < 2 && !err; i++)
  168. err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0);
  169. if (err < 0)
  170. return err;
  171. err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
  172. if (err < 0)
  173. return err;
  174. err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 2);
  175. if (err < 0)
  176. return err;
  177. for (i = 0; i < 2 && !err; i++)
  178. err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, 0);
  179. if (err < 0)
  180. return err;
  181. err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA,
  182. (width >> 8) & 0xff);
  183. if (err < 0)
  184. return err;
  185. err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, width & 0xff);
  186. if (err < 0)
  187. return err;
  188. err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0);
  189. if (err < 0)
  190. return err;
  191. switch (width) {
  192. case 640:
  193. PDEBUG(D_CONF, "Configuring camera for VGA mode");
  194. break;
  195. case 320:
  196. PDEBUG(D_CONF, "Configuring camera for QVGA mode");
  197. break;
  198. }
  199. return err;
  200. }
  201. void mt9m111_disconnect(struct sd *sd)
  202. {
  203. sd->sensor = NULL;
  204. }
  205. static int mt9m111_set_hvflip(struct gspca_dev *gspca_dev)
  206. {
  207. int err;
  208. u8 data[2] = {0x00, 0x00};
  209. struct sd *sd = (struct sd *) gspca_dev;
  210. int hflip;
  211. int vflip;
  212. PDEBUG(D_CONF, "Set hvflip to %d %d", sd->hflip->val, sd->vflip->val);
  213. /* The mt9m111 is flipped by default */
  214. hflip = !sd->hflip->val;
  215. vflip = !sd->vflip->val;
  216. /* Set the correct page map */
  217. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  218. if (err < 0)
  219. return err;
  220. data[0] = MT9M111_RMB_OVER_SIZED;
  221. if (gspca_dev->pixfmt.width == 640) {
  222. data[1] = MT9M111_RMB_ROW_SKIP_2X |
  223. MT9M111_RMB_COLUMN_SKIP_2X |
  224. (hflip << 1) | vflip;
  225. } else {
  226. data[1] = MT9M111_RMB_ROW_SKIP_4X |
  227. MT9M111_RMB_COLUMN_SKIP_4X |
  228. (hflip << 1) | vflip;
  229. }
  230. err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  231. data, 2);
  232. return err;
  233. }
  234. static int mt9m111_set_auto_white_balance(struct gspca_dev *gspca_dev,
  235. __s32 val)
  236. {
  237. struct sd *sd = (struct sd *) gspca_dev;
  238. int err;
  239. u8 data[2];
  240. err = m5602_read_sensor(sd, MT9M111_CP_OPERATING_MODE_CTL, data, 2);
  241. if (err < 0)
  242. return err;
  243. data[1] = ((data[1] & 0xfd) | ((val & 0x01) << 1));
  244. err = m5602_write_sensor(sd, MT9M111_CP_OPERATING_MODE_CTL, data, 2);
  245. PDEBUG(D_CONF, "Set auto white balance %d", val);
  246. return err;
  247. }
  248. static int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  249. {
  250. int err, tmp;
  251. u8 data[2] = {0x00, 0x00};
  252. struct sd *sd = (struct sd *) gspca_dev;
  253. /* Set the correct page map */
  254. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  255. if (err < 0)
  256. return err;
  257. if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2)
  258. return -EINVAL;
  259. if ((val >= INITIAL_MAX_GAIN * 2 * 2) &&
  260. (val < (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2))
  261. tmp = (1 << 10) | (val << 9) |
  262. (val << 8) | (val / 8);
  263. else if ((val >= INITIAL_MAX_GAIN * 2) &&
  264. (val < INITIAL_MAX_GAIN * 2 * 2))
  265. tmp = (1 << 9) | (1 << 8) | (val / 4);
  266. else if ((val >= INITIAL_MAX_GAIN) &&
  267. (val < INITIAL_MAX_GAIN * 2))
  268. tmp = (1 << 8) | (val / 2);
  269. else
  270. tmp = val;
  271. data[1] = (tmp & 0xff);
  272. data[0] = (tmp & 0xff00) >> 8;
  273. PDEBUG(D_CONF, "tmp=%d, data[1]=%d, data[0]=%d", tmp,
  274. data[1], data[0]);
  275. err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN,
  276. data, 2);
  277. return err;
  278. }
  279. static int mt9m111_set_green_balance(struct gspca_dev *gspca_dev, __s32 val)
  280. {
  281. int err;
  282. u8 data[2];
  283. struct sd *sd = (struct sd *) gspca_dev;
  284. data[1] = (val & 0xff);
  285. data[0] = (val & 0xff00) >> 8;
  286. PDEBUG(D_CONF, "Set green balance %d", val);
  287. err = m5602_write_sensor(sd, MT9M111_SC_GREEN_1_GAIN,
  288. data, 2);
  289. if (err < 0)
  290. return err;
  291. return m5602_write_sensor(sd, MT9M111_SC_GREEN_2_GAIN,
  292. data, 2);
  293. }
  294. static int mt9m111_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  295. {
  296. u8 data[2];
  297. struct sd *sd = (struct sd *) gspca_dev;
  298. data[1] = (val & 0xff);
  299. data[0] = (val & 0xff00) >> 8;
  300. PDEBUG(D_CONF, "Set blue balance %d", val);
  301. return m5602_write_sensor(sd, MT9M111_SC_BLUE_GAIN,
  302. data, 2);
  303. }
  304. static int mt9m111_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  305. {
  306. u8 data[2];
  307. struct sd *sd = (struct sd *) gspca_dev;
  308. data[1] = (val & 0xff);
  309. data[0] = (val & 0xff00) >> 8;
  310. PDEBUG(D_CONF, "Set red balance %d", val);
  311. return m5602_write_sensor(sd, MT9M111_SC_RED_GAIN,
  312. data, 2);
  313. }
  314. static int mt9m111_s_ctrl(struct v4l2_ctrl *ctrl)
  315. {
  316. struct gspca_dev *gspca_dev =
  317. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  318. struct sd *sd = (struct sd *) gspca_dev;
  319. int err;
  320. if (!gspca_dev->streaming)
  321. return 0;
  322. switch (ctrl->id) {
  323. case V4L2_CID_AUTO_WHITE_BALANCE:
  324. err = mt9m111_set_auto_white_balance(gspca_dev, ctrl->val);
  325. if (err || ctrl->val)
  326. return err;
  327. err = mt9m111_set_green_balance(gspca_dev, sd->green_bal->val);
  328. if (err)
  329. return err;
  330. err = mt9m111_set_red_balance(gspca_dev, sd->red_bal->val);
  331. if (err)
  332. return err;
  333. err = mt9m111_set_blue_balance(gspca_dev, sd->blue_bal->val);
  334. break;
  335. case V4L2_CID_GAIN:
  336. err = mt9m111_set_gain(gspca_dev, ctrl->val);
  337. break;
  338. case V4L2_CID_HFLIP:
  339. err = mt9m111_set_hvflip(gspca_dev);
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. return err;
  345. }
  346. static void mt9m111_dump_registers(struct sd *sd)
  347. {
  348. u8 address, value[2] = {0x00, 0x00};
  349. pr_info("Dumping the mt9m111 register state\n");
  350. pr_info("Dumping the mt9m111 sensor core registers\n");
  351. value[1] = MT9M111_SENSOR_CORE;
  352. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  353. for (address = 0; address < 0xff; address++) {
  354. m5602_read_sensor(sd, address, value, 2);
  355. pr_info("register 0x%x contains 0x%x%x\n",
  356. address, value[0], value[1]);
  357. }
  358. pr_info("Dumping the mt9m111 color pipeline registers\n");
  359. value[1] = MT9M111_COLORPIPE;
  360. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  361. for (address = 0; address < 0xff; address++) {
  362. m5602_read_sensor(sd, address, value, 2);
  363. pr_info("register 0x%x contains 0x%x%x\n",
  364. address, value[0], value[1]);
  365. }
  366. pr_info("Dumping the mt9m111 camera control registers\n");
  367. value[1] = MT9M111_CAMERA_CONTROL;
  368. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  369. for (address = 0; address < 0xff; address++) {
  370. m5602_read_sensor(sd, address, value, 2);
  371. pr_info("register 0x%x contains 0x%x%x\n",
  372. address, value[0], value[1]);
  373. }
  374. pr_info("mt9m111 register state dump complete\n");
  375. }