m5602_mt9m111.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 void mt9m111_dump_registers(struct sd *sd);
  20. int mt9m111_probe(struct sd *sd)
  21. {
  22. u8 data[2] = {0x00, 0x00};
  23. int i;
  24. if (force_sensor) {
  25. if (force_sensor == MT9M111_SENSOR) {
  26. info("Forcing a %s sensor", mt9m111.name);
  27. goto sensor_found;
  28. }
  29. /* If we want to force another sensor, don't try to probe this
  30. * one */
  31. return -ENODEV;
  32. }
  33. info("Probing for a mt9m111 sensor");
  34. /* Do the preinit */
  35. for (i = 0; i < ARRAY_SIZE(preinit_mt9m111); i++) {
  36. if (preinit_mt9m111[i][0] == BRIDGE) {
  37. m5602_write_bridge(sd,
  38. preinit_mt9m111[i][1],
  39. preinit_mt9m111[i][2]);
  40. } else {
  41. data[0] = preinit_mt9m111[i][2];
  42. data[1] = preinit_mt9m111[i][3];
  43. m5602_write_sensor(sd,
  44. preinit_mt9m111[i][1], data, 2);
  45. }
  46. }
  47. if (m5602_read_sensor(sd, MT9M111_SC_CHIPVER, data, 2))
  48. return -ENODEV;
  49. if ((data[0] == 0x14) && (data[1] == 0x3a)) {
  50. info("Detected a mt9m111 sensor");
  51. goto sensor_found;
  52. }
  53. return -ENODEV;
  54. sensor_found:
  55. sd->gspca_dev.cam.cam_mode = mt9m111.modes;
  56. sd->gspca_dev.cam.nmodes = mt9m111.nmodes;
  57. sd->desc->ctrls = mt9m111.ctrls;
  58. sd->desc->nctrls = mt9m111.nctrls;
  59. return 0;
  60. }
  61. int mt9m111_init(struct sd *sd)
  62. {
  63. int i, err = 0;
  64. /* Init the sensor */
  65. for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) {
  66. u8 data[2];
  67. if (init_mt9m111[i][0] == BRIDGE) {
  68. err = m5602_write_bridge(sd,
  69. init_mt9m111[i][1],
  70. init_mt9m111[i][2]);
  71. } else {
  72. data[0] = init_mt9m111[i][2];
  73. data[1] = init_mt9m111[i][3];
  74. err = m5602_write_sensor(sd,
  75. init_mt9m111[i][1], data, 2);
  76. }
  77. }
  78. if (dump_sensor)
  79. mt9m111_dump_registers(sd);
  80. return (err < 0) ? err : 0;
  81. }
  82. int mt9m111_power_down(struct sd *sd)
  83. {
  84. return 0;
  85. }
  86. int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
  87. {
  88. int err;
  89. u8 data[2] = {0x00, 0x00};
  90. struct sd *sd = (struct sd *) gspca_dev;
  91. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  92. data, 2);
  93. *val = data[0] & MT9M111_RMB_MIRROR_ROWS;
  94. PDEBUG(D_V4L2, "Read vertical flip %d", *val);
  95. return err;
  96. }
  97. int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  98. {
  99. int err;
  100. u8 data[2] = {0x00, 0x00};
  101. struct sd *sd = (struct sd *) gspca_dev;
  102. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  103. /* Set the correct page map */
  104. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  105. if (err < 0)
  106. goto out;
  107. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
  108. if (err < 0)
  109. goto out;
  110. data[0] = (data[0] & 0xfe) | val;
  111. err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  112. data, 2);
  113. out:
  114. return err;
  115. }
  116. int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
  117. {
  118. int err;
  119. u8 data[2] = {0x00, 0x00};
  120. struct sd *sd = (struct sd *) gspca_dev;
  121. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  122. data, 2);
  123. *val = data[0] & MT9M111_RMB_MIRROR_COLS;
  124. PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
  125. return err;
  126. }
  127. int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  128. {
  129. int err;
  130. u8 data[2] = {0x00, 0x00};
  131. struct sd *sd = (struct sd *) gspca_dev;
  132. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  133. /* Set the correct page map */
  134. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  135. if (err < 0)
  136. goto out;
  137. err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
  138. if (err < 0)
  139. goto out;
  140. data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02);
  141. err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
  142. data, 2);
  143. out:
  144. return err;
  145. }
  146. int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  147. {
  148. int err, tmp;
  149. u8 data[2] = {0x00, 0x00};
  150. struct sd *sd = (struct sd *) gspca_dev;
  151. err = m5602_read_sensor(sd, MT9M111_SC_GLOBAL_GAIN, data, 2);
  152. tmp = ((data[1] << 8) | data[0]);
  153. *val = ((tmp & (1 << 10)) * 2) |
  154. ((tmp & (1 << 9)) * 2) |
  155. ((tmp & (1 << 8)) * 2) |
  156. (tmp & 0x7f);
  157. PDEBUG(D_V4L2, "Read gain %d", *val);
  158. return err;
  159. }
  160. int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  161. {
  162. int err, tmp;
  163. u8 data[2] = {0x00, 0x00};
  164. struct sd *sd = (struct sd *) gspca_dev;
  165. /* Set the correct page map */
  166. err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
  167. if (err < 0)
  168. goto out;
  169. if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2)
  170. return -EINVAL;
  171. if ((val >= INITIAL_MAX_GAIN * 2 * 2) &&
  172. (val < (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2))
  173. tmp = (1 << 10) | (val << 9) |
  174. (val << 8) | (val / 8);
  175. else if ((val >= INITIAL_MAX_GAIN * 2) &&
  176. (val < INITIAL_MAX_GAIN * 2 * 2))
  177. tmp = (1 << 9) | (1 << 8) | (val / 4);
  178. else if ((val >= INITIAL_MAX_GAIN) &&
  179. (val < INITIAL_MAX_GAIN * 2))
  180. tmp = (1 << 8) | (val / 2);
  181. else
  182. tmp = val;
  183. data[1] = (tmp & 0xff00) >> 8;
  184. data[0] = (tmp & 0xff);
  185. PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp,
  186. data[1], data[0]);
  187. err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN,
  188. data, 2);
  189. out:
  190. return err;
  191. }
  192. static void mt9m111_dump_registers(struct sd *sd)
  193. {
  194. u8 address, value[2] = {0x00, 0x00};
  195. info("Dumping the mt9m111 register state");
  196. info("Dumping the mt9m111 sensor core registers");
  197. value[1] = MT9M111_SENSOR_CORE;
  198. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  199. for (address = 0; address < 0xff; address++) {
  200. m5602_read_sensor(sd, address, value, 2);
  201. info("register 0x%x contains 0x%x%x",
  202. address, value[0], value[1]);
  203. }
  204. info("Dumping the mt9m111 color pipeline registers");
  205. value[1] = MT9M111_COLORPIPE;
  206. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  207. for (address = 0; address < 0xff; address++) {
  208. m5602_read_sensor(sd, address, value, 2);
  209. info("register 0x%x contains 0x%x%x",
  210. address, value[0], value[1]);
  211. }
  212. info("Dumping the mt9m111 camera control registers");
  213. value[1] = MT9M111_CAMERA_CONTROL;
  214. m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
  215. for (address = 0; address < 0xff; address++) {
  216. m5602_read_sensor(sd, address, value, 2);
  217. info("register 0x%x contains 0x%x%x",
  218. address, value[0], value[1]);
  219. }
  220. info("mt9m111 register state dump complete");
  221. }