stv06xx_vv6410.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 cam *cam = &sd->gspca_dev.cam;
  117. u32 priv = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
  118. if (priv & VV6410_SUBSAMPLE) {
  119. PDEBUG(D_CONF, "Enabling subsampling");
  120. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02);
  121. stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
  122. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
  123. } else {
  124. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
  125. stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
  126. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x00);
  127. }
  128. /* Turn on LED */
  129. err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_ON);
  130. if (err < 0)
  131. return err;
  132. err = stv06xx_write_sensor(sd, VV6410_SETUP0, 0);
  133. if (err < 0)
  134. return err;
  135. PDEBUG(D_STREAM, "Starting stream");
  136. return 0;
  137. }
  138. static int vv6410_stop(struct sd *sd)
  139. {
  140. int err;
  141. /* Turn off LED */
  142. err = stv06xx_write_bridge(sd, STV_LED_CTRL, LED_OFF);
  143. if (err < 0)
  144. return err;
  145. err = stv06xx_write_sensor(sd, VV6410_SETUP0, VV6410_LOW_POWER_MODE);
  146. if (err < 0)
  147. return err;
  148. PDEBUG(D_STREAM, "Halting stream");
  149. return (err < 0) ? err : 0;
  150. }
  151. static int vv6410_dump(struct sd *sd)
  152. {
  153. u8 i;
  154. int err = 0;
  155. pr_info("Dumping all vv6410 sensor registers\n");
  156. for (i = 0; i < 0xff && !err; i++) {
  157. u16 data;
  158. err = stv06xx_read_sensor(sd, i, &data);
  159. pr_info("Register 0x%x contained 0x%x\n", i, data);
  160. }
  161. return (err < 0) ? err : 0;
  162. }
  163. static int vv6410_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
  164. {
  165. int err;
  166. u16 i2c_data;
  167. struct sd *sd = (struct sd *) gspca_dev;
  168. err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
  169. if (err < 0)
  170. return err;
  171. if (val)
  172. i2c_data |= VV6410_HFLIP;
  173. else
  174. i2c_data &= ~VV6410_HFLIP;
  175. PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
  176. err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
  177. return (err < 0) ? err : 0;
  178. }
  179. static int vv6410_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
  180. {
  181. int err;
  182. u16 i2c_data;
  183. struct sd *sd = (struct sd *) gspca_dev;
  184. err = stv06xx_read_sensor(sd, VV6410_DATAFORMAT, &i2c_data);
  185. if (err < 0)
  186. return err;
  187. if (val)
  188. i2c_data |= VV6410_VFLIP;
  189. else
  190. i2c_data &= ~VV6410_VFLIP;
  191. PDEBUG(D_V4L2, "Set vertical flip to %d", val);
  192. err = stv06xx_write_sensor(sd, VV6410_DATAFORMAT, i2c_data);
  193. return (err < 0) ? err : 0;
  194. }
  195. static int vv6410_set_analog_gain(struct gspca_dev *gspca_dev, __s32 val)
  196. {
  197. int err;
  198. struct sd *sd = (struct sd *) gspca_dev;
  199. PDEBUG(D_V4L2, "Set analog gain to %d", val);
  200. err = stv06xx_write_sensor(sd, VV6410_ANALOGGAIN, 0xf0 | (val & 0xf));
  201. return (err < 0) ? err : 0;
  202. }
  203. static int vv6410_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  204. {
  205. int err;
  206. struct sd *sd = (struct sd *) gspca_dev;
  207. unsigned int fine, coarse;
  208. val = (val * val >> 14) + val / 4;
  209. fine = val % VV6410_CIF_LINELENGTH;
  210. coarse = min(512, val / VV6410_CIF_LINELENGTH);
  211. PDEBUG(D_V4L2, "Set coarse exposure to %d, fine expsure to %d",
  212. coarse, fine);
  213. err = stv06xx_write_sensor(sd, VV6410_FINEH, fine >> 8);
  214. if (err < 0)
  215. goto out;
  216. err = stv06xx_write_sensor(sd, VV6410_FINEL, fine & 0xff);
  217. if (err < 0)
  218. goto out;
  219. err = stv06xx_write_sensor(sd, VV6410_COARSEH, coarse >> 8);
  220. if (err < 0)
  221. goto out;
  222. err = stv06xx_write_sensor(sd, VV6410_COARSEL, coarse & 0xff);
  223. out:
  224. return err;
  225. }