m5602_ov7660.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Driver for the ov7660 sensor
  3. *
  4. * Copyright (C) 2009 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_ov7660.h"
  20. static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl);
  21. static void ov7660_dump_registers(struct sd *sd);
  22. static struct v4l2_pix_format ov7660_modes[] = {
  23. {
  24. 640,
  25. 480,
  26. V4L2_PIX_FMT_SBGGR8,
  27. V4L2_FIELD_NONE,
  28. .sizeimage =
  29. 640 * 480,
  30. .bytesperline = 640,
  31. .colorspace = V4L2_COLORSPACE_SRGB,
  32. .priv = 0
  33. }
  34. };
  35. static const struct v4l2_ctrl_ops ov7660_ctrl_ops = {
  36. .s_ctrl = ov7660_s_ctrl,
  37. };
  38. int ov7660_probe(struct sd *sd)
  39. {
  40. int err = 0, i;
  41. u8 prod_id = 0, ver_id = 0;
  42. if (force_sensor) {
  43. if (force_sensor == OV7660_SENSOR) {
  44. pr_info("Forcing an %s sensor\n", ov7660.name);
  45. goto sensor_found;
  46. }
  47. /* If we want to force another sensor,
  48. don't try to probe this one */
  49. return -ENODEV;
  50. }
  51. /* Do the preinit */
  52. for (i = 0; i < ARRAY_SIZE(preinit_ov7660) && !err; i++) {
  53. u8 data[2];
  54. if (preinit_ov7660[i][0] == BRIDGE) {
  55. err = m5602_write_bridge(sd,
  56. preinit_ov7660[i][1],
  57. preinit_ov7660[i][2]);
  58. } else {
  59. data[0] = preinit_ov7660[i][2];
  60. err = m5602_write_sensor(sd,
  61. preinit_ov7660[i][1], data, 1);
  62. }
  63. }
  64. if (err < 0)
  65. return err;
  66. if (m5602_read_sensor(sd, OV7660_PID, &prod_id, 1))
  67. return -ENODEV;
  68. if (m5602_read_sensor(sd, OV7660_VER, &ver_id, 1))
  69. return -ENODEV;
  70. pr_info("Sensor reported 0x%x%x\n", prod_id, ver_id);
  71. if ((prod_id == 0x76) && (ver_id == 0x60)) {
  72. pr_info("Detected a ov7660 sensor\n");
  73. goto sensor_found;
  74. }
  75. return -ENODEV;
  76. sensor_found:
  77. sd->gspca_dev.cam.cam_mode = ov7660_modes;
  78. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(ov7660_modes);
  79. return 0;
  80. }
  81. int ov7660_init(struct sd *sd)
  82. {
  83. int i, err;
  84. /* Init the sensor */
  85. for (i = 0; i < ARRAY_SIZE(init_ov7660); i++) {
  86. u8 data[2];
  87. if (init_ov7660[i][0] == BRIDGE) {
  88. err = m5602_write_bridge(sd,
  89. init_ov7660[i][1],
  90. init_ov7660[i][2]);
  91. } else {
  92. data[0] = init_ov7660[i][2];
  93. err = m5602_write_sensor(sd,
  94. init_ov7660[i][1], data, 1);
  95. }
  96. if (err < 0)
  97. return err;
  98. }
  99. if (dump_sensor)
  100. ov7660_dump_registers(sd);
  101. return 0;
  102. }
  103. int ov7660_init_controls(struct sd *sd)
  104. {
  105. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  106. sd->gspca_dev.vdev.ctrl_handler = hdl;
  107. v4l2_ctrl_handler_init(hdl, 6);
  108. v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
  109. 0, 1, 1, 1);
  110. v4l2_ctrl_new_std_menu(hdl, &ov7660_ctrl_ops,
  111. V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_AUTO);
  112. sd->autogain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops,
  113. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  114. sd->gain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_GAIN, 0,
  115. 255, 1, OV7660_DEFAULT_GAIN);
  116. sd->hflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_HFLIP,
  117. 0, 1, 1, 0);
  118. sd->vflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_VFLIP,
  119. 0, 1, 1, 0);
  120. if (hdl->error) {
  121. pr_err("Could not initialize controls\n");
  122. return hdl->error;
  123. }
  124. v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, false);
  125. v4l2_ctrl_cluster(2, &sd->hflip);
  126. return 0;
  127. }
  128. int ov7660_start(struct sd *sd)
  129. {
  130. return 0;
  131. }
  132. int ov7660_stop(struct sd *sd)
  133. {
  134. return 0;
  135. }
  136. void ov7660_disconnect(struct sd *sd)
  137. {
  138. ov7660_stop(sd);
  139. sd->sensor = NULL;
  140. }
  141. static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  142. {
  143. int err;
  144. u8 i2c_data = val;
  145. struct sd *sd = (struct sd *) gspca_dev;
  146. PDEBUG(D_CONF, "Setting gain to %d", val);
  147. err = m5602_write_sensor(sd, OV7660_GAIN, &i2c_data, 1);
  148. return err;
  149. }
  150. static int ov7660_set_auto_white_balance(struct gspca_dev *gspca_dev,
  151. __s32 val)
  152. {
  153. int err;
  154. u8 i2c_data;
  155. struct sd *sd = (struct sd *) gspca_dev;
  156. PDEBUG(D_CONF, "Set auto white balance to %d", val);
  157. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  158. if (err < 0)
  159. return err;
  160. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  161. err = m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  162. return err;
  163. }
  164. static int ov7660_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  165. {
  166. int err;
  167. u8 i2c_data;
  168. struct sd *sd = (struct sd *) gspca_dev;
  169. PDEBUG(D_CONF, "Set auto gain control to %d", val);
  170. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  171. if (err < 0)
  172. return err;
  173. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  174. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  175. }
  176. static int ov7660_set_auto_exposure(struct gspca_dev *gspca_dev,
  177. __s32 val)
  178. {
  179. int err;
  180. u8 i2c_data;
  181. struct sd *sd = (struct sd *) gspca_dev;
  182. PDEBUG(D_CONF, "Set auto exposure control to %d", val);
  183. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  184. if (err < 0)
  185. return err;
  186. val = (val == V4L2_EXPOSURE_AUTO);
  187. i2c_data = ((i2c_data & 0xfe) | ((val & 0x01) << 0));
  188. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  189. }
  190. static int ov7660_set_hvflip(struct gspca_dev *gspca_dev)
  191. {
  192. int err;
  193. u8 i2c_data;
  194. struct sd *sd = (struct sd *) gspca_dev;
  195. PDEBUG(D_CONF, "Set hvflip to %d, %d", sd->hflip->val, sd->vflip->val);
  196. i2c_data = (sd->hflip->val << 5) | (sd->vflip->val << 4);
  197. err = m5602_write_sensor(sd, OV7660_MVFP, &i2c_data, 1);
  198. return err;
  199. }
  200. static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl)
  201. {
  202. struct gspca_dev *gspca_dev =
  203. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  204. struct sd *sd = (struct sd *) gspca_dev;
  205. int err;
  206. if (!gspca_dev->streaming)
  207. return 0;
  208. switch (ctrl->id) {
  209. case V4L2_CID_AUTO_WHITE_BALANCE:
  210. err = ov7660_set_auto_white_balance(gspca_dev, ctrl->val);
  211. break;
  212. case V4L2_CID_EXPOSURE_AUTO:
  213. err = ov7660_set_auto_exposure(gspca_dev, ctrl->val);
  214. break;
  215. case V4L2_CID_AUTOGAIN:
  216. err = ov7660_set_auto_gain(gspca_dev, ctrl->val);
  217. if (err || ctrl->val)
  218. return err;
  219. err = ov7660_set_gain(gspca_dev, sd->gain->val);
  220. break;
  221. case V4L2_CID_HFLIP:
  222. err = ov7660_set_hvflip(gspca_dev);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. return err;
  228. }
  229. static void ov7660_dump_registers(struct sd *sd)
  230. {
  231. int address;
  232. pr_info("Dumping the ov7660 register state\n");
  233. for (address = 0; address < 0xa9; address++) {
  234. u8 value;
  235. m5602_read_sensor(sd, address, &value, 1);
  236. pr_info("register 0x%x contains 0x%x\n", address, value);
  237. }
  238. pr_info("ov7660 register state dump complete\n");
  239. pr_info("Probing for which registers that are read/write\n");
  240. for (address = 0; address < 0xff; address++) {
  241. u8 old_value, ctrl_value;
  242. u8 test_value[2] = {0xff, 0xff};
  243. m5602_read_sensor(sd, address, &old_value, 1);
  244. m5602_write_sensor(sd, address, test_value, 1);
  245. m5602_read_sensor(sd, address, &ctrl_value, 1);
  246. if (ctrl_value == test_value[0])
  247. pr_info("register 0x%x is writeable\n", address);
  248. else
  249. pr_info("register 0x%x is read only\n", address);
  250. /* Restore original value */
  251. m5602_write_sensor(sd, address, &old_value, 1);
  252. }
  253. }