m5602_ov7660.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 = 0;
  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. }
  97. if (dump_sensor)
  98. ov7660_dump_registers(sd);
  99. return 0;
  100. }
  101. int ov7660_init_controls(struct sd *sd)
  102. {
  103. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  104. sd->gspca_dev.vdev.ctrl_handler = hdl;
  105. v4l2_ctrl_handler_init(hdl, 6);
  106. v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_AUTO_WHITE_BALANCE,
  107. 0, 1, 1, 1);
  108. v4l2_ctrl_new_std_menu(hdl, &ov7660_ctrl_ops,
  109. V4L2_CID_EXPOSURE_AUTO, 1, 0, V4L2_EXPOSURE_AUTO);
  110. sd->autogain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops,
  111. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  112. sd->gain = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_GAIN, 0,
  113. 255, 1, OV7660_DEFAULT_GAIN);
  114. sd->hflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_HFLIP,
  115. 0, 1, 1, 0);
  116. sd->vflip = v4l2_ctrl_new_std(hdl, &ov7660_ctrl_ops, V4L2_CID_VFLIP,
  117. 0, 1, 1, 0);
  118. if (hdl->error) {
  119. pr_err("Could not initialize controls\n");
  120. return hdl->error;
  121. }
  122. v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, false);
  123. v4l2_ctrl_cluster(2, &sd->hflip);
  124. return 0;
  125. }
  126. int ov7660_start(struct sd *sd)
  127. {
  128. return 0;
  129. }
  130. int ov7660_stop(struct sd *sd)
  131. {
  132. return 0;
  133. }
  134. void ov7660_disconnect(struct sd *sd)
  135. {
  136. ov7660_stop(sd);
  137. sd->sensor = NULL;
  138. }
  139. static int ov7660_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  140. {
  141. int err;
  142. u8 i2c_data = val;
  143. struct sd *sd = (struct sd *) gspca_dev;
  144. PDEBUG(D_V4L2, "Setting gain to %d", val);
  145. err = m5602_write_sensor(sd, OV7660_GAIN, &i2c_data, 1);
  146. return err;
  147. }
  148. static int ov7660_set_auto_white_balance(struct gspca_dev *gspca_dev,
  149. __s32 val)
  150. {
  151. int err;
  152. u8 i2c_data;
  153. struct sd *sd = (struct sd *) gspca_dev;
  154. PDEBUG(D_V4L2, "Set auto white balance to %d", val);
  155. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  156. if (err < 0)
  157. return err;
  158. i2c_data = ((i2c_data & 0xfd) | ((val & 0x01) << 1));
  159. err = m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  160. return err;
  161. }
  162. static int ov7660_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val)
  163. {
  164. int err;
  165. u8 i2c_data;
  166. struct sd *sd = (struct sd *) gspca_dev;
  167. PDEBUG(D_V4L2, "Set auto gain control to %d", val);
  168. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  169. if (err < 0)
  170. return err;
  171. i2c_data = ((i2c_data & 0xfb) | ((val & 0x01) << 2));
  172. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  173. }
  174. static int ov7660_set_auto_exposure(struct gspca_dev *gspca_dev,
  175. __s32 val)
  176. {
  177. int err;
  178. u8 i2c_data;
  179. struct sd *sd = (struct sd *) gspca_dev;
  180. PDEBUG(D_V4L2, "Set auto exposure control to %d", val);
  181. err = m5602_read_sensor(sd, OV7660_COM8, &i2c_data, 1);
  182. if (err < 0)
  183. return err;
  184. val = (val == V4L2_EXPOSURE_AUTO);
  185. i2c_data = ((i2c_data & 0xfe) | ((val & 0x01) << 0));
  186. return m5602_write_sensor(sd, OV7660_COM8, &i2c_data, 1);
  187. }
  188. static int ov7660_set_hvflip(struct gspca_dev *gspca_dev)
  189. {
  190. int err;
  191. u8 i2c_data;
  192. struct sd *sd = (struct sd *) gspca_dev;
  193. PDEBUG(D_V4L2, "Set hvflip to %d, %d", sd->hflip->val, sd->vflip->val);
  194. i2c_data = (sd->hflip->val << 5) | (sd->vflip->val << 4);
  195. err = m5602_write_sensor(sd, OV7660_MVFP, &i2c_data, 1);
  196. return err;
  197. }
  198. static int ov7660_s_ctrl(struct v4l2_ctrl *ctrl)
  199. {
  200. struct gspca_dev *gspca_dev =
  201. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  202. struct sd *sd = (struct sd *) gspca_dev;
  203. int err;
  204. if (!gspca_dev->streaming)
  205. return 0;
  206. switch (ctrl->id) {
  207. case V4L2_CID_AUTO_WHITE_BALANCE:
  208. err = ov7660_set_auto_white_balance(gspca_dev, ctrl->val);
  209. break;
  210. case V4L2_CID_EXPOSURE_AUTO:
  211. err = ov7660_set_auto_exposure(gspca_dev, ctrl->val);
  212. break;
  213. case V4L2_CID_AUTOGAIN:
  214. err = ov7660_set_auto_gain(gspca_dev, ctrl->val);
  215. if (err || ctrl->val)
  216. return err;
  217. err = ov7660_set_gain(gspca_dev, sd->gain->val);
  218. break;
  219. case V4L2_CID_HFLIP:
  220. err = ov7660_set_hvflip(gspca_dev);
  221. break;
  222. default:
  223. return -EINVAL;
  224. }
  225. return err;
  226. }
  227. static void ov7660_dump_registers(struct sd *sd)
  228. {
  229. int address;
  230. pr_info("Dumping the ov7660 register state\n");
  231. for (address = 0; address < 0xa9; address++) {
  232. u8 value;
  233. m5602_read_sensor(sd, address, &value, 1);
  234. pr_info("register 0x%x contains 0x%x\n", address, value);
  235. }
  236. pr_info("ov7660 register state dump complete\n");
  237. pr_info("Probing for which registers that are read/write\n");
  238. for (address = 0; address < 0xff; address++) {
  239. u8 old_value, ctrl_value;
  240. u8 test_value[2] = {0xff, 0xff};
  241. m5602_read_sensor(sd, address, &old_value, 1);
  242. m5602_write_sensor(sd, address, test_value, 1);
  243. m5602_read_sensor(sd, address, &ctrl_value, 1);
  244. if (ctrl_value == test_value[0])
  245. pr_info("register 0x%x is writeable\n", address);
  246. else
  247. pr_info("register 0x%x is read only\n", address);
  248. /* Restore original value */
  249. m5602_write_sensor(sd, address, &old_value, 1);
  250. }
  251. }