stv06xx_vv6410.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
  3. * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
  4. * Copyright (c) 2002, 2003 Tuukka Toivonen
  5. * Copyright (c) 2008 Erik Andrén
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * P/N 861037: Sensor HDCS1000 ASIC STV0600
  22. * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
  23. * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
  24. * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
  25. * P/N 861075-0040: Sensor HDCS1000 ASIC
  26. * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
  27. * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include "stv06xx_vv6410.h"
  31. static struct v4l2_pix_format vv6410_mode[] = {
  32. {
  33. 356,
  34. 292,
  35. V4L2_PIX_FMT_SGRBG8,
  36. V4L2_FIELD_NONE,
  37. .sizeimage = 356 * 292,
  38. .bytesperline = 356,
  39. .colorspace = V4L2_COLORSPACE_SRGB,
  40. .priv = 0
  41. }
  42. };
  43. static int vv6410_s_ctrl(struct v4l2_ctrl *ctrl)
  44. {
  45. struct gspca_dev *gspca_dev =
  46. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  47. int err = -EINVAL;
  48. switch (ctrl->id) {
  49. case V4L2_CID_HFLIP:
  50. if (!gspca_dev->streaming)
  51. return 0;
  52. err = vv6410_set_hflip(gspca_dev, ctrl->val);
  53. break;
  54. case V4L2_CID_VFLIP:
  55. if (!gspca_dev->streaming)
  56. return 0;
  57. err = vv6410_set_vflip(gspca_dev, ctrl->val);
  58. break;
  59. case V4L2_CID_GAIN:
  60. err = vv6410_set_analog_gain(gspca_dev, ctrl->val);
  61. break;
  62. case V4L2_CID_EXPOSURE:
  63. err = vv6410_set_exposure(gspca_dev, ctrl->val);
  64. break;
  65. }
  66. return err;
  67. }
  68. static const struct v4l2_ctrl_ops vv6410_ctrl_ops = {
  69. .s_ctrl = vv6410_s_ctrl,
  70. };
  71. static int vv6410_probe(struct sd *sd)
  72. {
  73. u16 data;
  74. int err;
  75. err = stv06xx_read_sensor(sd, VV6410_DEVICEH, &data);
  76. if (err < 0)
  77. return -ENODEV;
  78. if (data != 0x19)
  79. return -ENODEV;
  80. pr_info("vv6410 sensor detected\n");
  81. sd->gspca_dev.cam.cam_mode = vv6410_mode;
  82. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(vv6410_mode);
  83. return 0;
  84. }
  85. static int vv6410_init_controls(struct sd *sd)
  86. {
  87. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  88. v4l2_ctrl_handler_init(hdl, 2);
  89. /* Disable the hardware VFLIP and HFLIP as we currently lack a
  90. mechanism to adjust the image offset in such a way that
  91. we don't need to renegotiate the announced format */
  92. /* v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops, */
  93. /* V4L2_CID_HFLIP, 0, 1, 1, 0); */
  94. /* v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops, */
  95. /* V4L2_CID_VFLIP, 0, 1, 1, 0); */
  96. v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops,
  97. V4L2_CID_EXPOSURE, 0, 32768, 1, 20000);
  98. v4l2_ctrl_new_std(hdl, &vv6410_ctrl_ops,
  99. V4L2_CID_GAIN, 0, 15, 1, 10);
  100. return hdl->error;
  101. }
  102. static int vv6410_init(struct sd *sd)
  103. {
  104. int err = 0, i;
  105. for (i = 0; i < ARRAY_SIZE(stv_bridge_init); i++)
  106. stv06xx_write_bridge(sd, stv_bridge_init[i].addr, stv_bridge_init[i].data);
  107. if (err < 0)
  108. return err;
  109. err = stv06xx_write_sensor_bytes(sd, (u8 *) vv6410_sensor_init,
  110. ARRAY_SIZE(vv6410_sensor_init));
  111. return (err < 0) ? err : 0;
  112. }
  113. static int vv6410_start(struct sd *sd)
  114. {
  115. int err;
  116. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  117. struct cam *cam = &sd->gspca_dev.cam;
  118. u32 priv = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
  119. if (priv & VV6410_SUBSAMPLE) {
  120. PDEBUG(D_CONF, "Enabling subsampling");
  121. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02);
  122. stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
  123. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
  124. } else {
  125. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
  126. stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
  127. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x00);
  128. }
  129. /* Turn on LED */
  130. err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_ON);
  131. if (err < 0)
  132. return err;
  133. err = stv06xx_write_sensor(sd, VV6410_SETUP0, 0);
  134. if (err < 0)
  135. return err;
  136. PDEBUG(D_STREAM, "Starting stream");
  137. return 0;
  138. }
  139. static int vv6410_stop(struct sd *sd)
  140. {
  141. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  142. int err;
  143. /* Turn off LED */
  144. err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_OFF);
  145. if (err < 0)
  146. return err;
  147. err = stv06xx_write_sensor(sd, VV6410_SETUP0, VV6410_LOW_POWER_MODE);
  148. if (err < 0)
  149. return err;
  150. PDEBUG(D_STREAM, "Halting stream");
  151. return 0;
  152. }
  153. static int vv6410_dump(struct sd *sd)
  154. {
  155. u8 i;
  156. int err = 0;
  157. pr_info("Dumping all vv6410 sensor registers\n");
  158. for (i = 0; i < 0xff && !err; i++) {
  159. u16 data;
  160. err = stv06xx_read_sensor(sd, i, &data);
  161. pr_info("Register 0x%x contained 0x%x\n", i, data);
  162. }
  163. return (err < 0) ? err : 0;
  164. }
  165. static int vv6410_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  166. {
  167. int err;
  168. u16 i2c_data;
  169. struct sd *sd = (struct sd *) gspca_dev;
  170. err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
  171. if (err < 0)
  172. return err;
  173. if (val)
  174. i2c_data |= VV6410_HFLIP;
  175. else
  176. i2c_data &= ~VV6410_HFLIP;
  177. PDEBUG(D_CONF, "Set horizontal flip to %d", val);
  178. err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
  179. return (err < 0) ? err : 0;
  180. }
  181. static int vv6410_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  182. {
  183. int err;
  184. u16 i2c_data;
  185. struct sd *sd = (struct sd *) gspca_dev;
  186. err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
  187. if (err < 0)
  188. return err;
  189. if (val)
  190. i2c_data |= VV6410_VFLIP;
  191. else
  192. i2c_data &= ~VV6410_VFLIP;
  193. PDEBUG(D_CONF, "Set vertical flip to %d", val);
  194. err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
  195. return (err < 0) ? err : 0;
  196. }
  197. static int vv6410_set_analog_gain(struct gspca_dev *gspca_dev, __s32 val)
  198. {
  199. int err;
  200. struct sd *sd = (struct sd *) gspca_dev;
  201. PDEBUG(D_CONF, "Set analog gain to %d", val);
  202. err = stv06xx_write_sensor(sd, VV6410_ANALOGGAIN, 0xf0 | (val & 0xf));
  203. return (err < 0) ? err : 0;
  204. }
  205. static int vv6410_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  206. {
  207. int err;
  208. struct sd *sd = (struct sd *) gspca_dev;
  209. unsigned int fine, coarse;
  210. val = (val * val >> 14) + val / 4;
  211. fine = val % VV6410_CIF_LINELENGTH;
  212. coarse = min(512, val / VV6410_CIF_LINELENGTH);
  213. PDEBUG(D_CONF, "Set coarse exposure to %d, fine exposure to %d",
  214. coarse, fine);
  215. err = stv06xx_write_sensor(sd, VV6410_FINEH, fine >> 8);
  216. if (err < 0)
  217. goto out;
  218. err = stv06xx_write_sensor(sd, VV6410_FINEL, fine & 0xff);
  219. if (err < 0)
  220. goto out;
  221. err = stv06xx_write_sensor(sd, VV6410_COARSEH, coarse >> 8);
  222. if (err < 0)
  223. goto out;
  224. err = stv06xx_write_sensor(sd, VV6410_COARSEL, coarse & 0xff);
  225. out:
  226. return err;
  227. }