stv06xx_pb0100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. /*
  30. * The spec file for the PB-0100 suggests the following for best quality
  31. * images after the sensor has been reset :
  32. *
  33. * PB_ADCGAINL = R60 = 0x03 (3 dec) : sets low reference of ADC
  34. to produce good black level
  35. * PB_PREADCTRL = R32 = 0x1400 (5120 dec) : Enables global gain changes
  36. through R53
  37. * PB_ADCMINGAIN = R52 = 0x10 (16 dec) : Sets the minimum gain for
  38. auto-exposure
  39. * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec) : Sets the global gain
  40. * PB_EXPGAIN = R14 = 0x11 (17 dec) : Sets the auto-exposure value
  41. * PB_UPDATEINT = R23 = 0x02 (2 dec) : Sets the speed on
  42. auto-exposure routine
  43. * PB_CFILLIN = R5 = 0x0E (14 dec) : Sets the frame rate
  44. */
  45. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  46. #include "stv06xx_pb0100.h"
  47. struct pb0100_ctrls {
  48. struct { /* one big happy control cluster... */
  49. struct v4l2_ctrl *autogain;
  50. struct v4l2_ctrl *gain;
  51. struct v4l2_ctrl *exposure;
  52. struct v4l2_ctrl *red;
  53. struct v4l2_ctrl *blue;
  54. struct v4l2_ctrl *natural;
  55. };
  56. struct v4l2_ctrl *target;
  57. };
  58. static struct v4l2_pix_format pb0100_mode[] = {
  59. /* low res / subsample modes disabled as they are only half res horizontal,
  60. halving the vertical resolution does not seem to work */
  61. {
  62. 320,
  63. 240,
  64. V4L2_PIX_FMT_SGRBG8,
  65. V4L2_FIELD_NONE,
  66. .sizeimage = 320 * 240,
  67. .bytesperline = 320,
  68. .colorspace = V4L2_COLORSPACE_SRGB,
  69. .priv = PB0100_CROP_TO_VGA
  70. },
  71. {
  72. 352,
  73. 288,
  74. V4L2_PIX_FMT_SGRBG8,
  75. V4L2_FIELD_NONE,
  76. .sizeimage = 352 * 288,
  77. .bytesperline = 352,
  78. .colorspace = V4L2_COLORSPACE_SRGB,
  79. .priv = 0
  80. }
  81. };
  82. static int pb0100_s_ctrl(struct v4l2_ctrl *ctrl)
  83. {
  84. struct gspca_dev *gspca_dev =
  85. container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
  86. struct sd *sd = (struct sd *)gspca_dev;
  87. struct pb0100_ctrls *ctrls = sd->sensor_priv;
  88. int err = -EINVAL;
  89. switch (ctrl->id) {
  90. case V4L2_CID_AUTOGAIN:
  91. err = pb0100_set_autogain(gspca_dev, ctrl->val);
  92. if (err)
  93. break;
  94. if (ctrl->val)
  95. break;
  96. err = pb0100_set_gain(gspca_dev, ctrls->gain->val);
  97. if (err)
  98. break;
  99. err = pb0100_set_exposure(gspca_dev, ctrls->exposure->val);
  100. break;
  101. case V4L2_CTRL_CLASS_USER + 0x1001:
  102. err = pb0100_set_autogain_target(gspca_dev, ctrl->val);
  103. break;
  104. }
  105. return err;
  106. }
  107. static const struct v4l2_ctrl_ops pb0100_ctrl_ops = {
  108. .s_ctrl = pb0100_s_ctrl,
  109. };
  110. static int pb0100_init_controls(struct sd *sd)
  111. {
  112. struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
  113. struct pb0100_ctrls *ctrls;
  114. static const struct v4l2_ctrl_config autogain_target = {
  115. .ops = &pb0100_ctrl_ops,
  116. .id = V4L2_CTRL_CLASS_USER + 0x1000,
  117. .type = V4L2_CTRL_TYPE_INTEGER,
  118. .name = "Automatic Gain Target",
  119. .max = 255,
  120. .step = 1,
  121. .def = 128,
  122. };
  123. static const struct v4l2_ctrl_config natural_light = {
  124. .ops = &pb0100_ctrl_ops,
  125. .id = V4L2_CTRL_CLASS_USER + 0x1001,
  126. .type = V4L2_CTRL_TYPE_BOOLEAN,
  127. .name = "Natural Light Source",
  128. .max = 1,
  129. .step = 1,
  130. .def = 1,
  131. };
  132. ctrls = kzalloc(sizeof(*ctrls), GFP_KERNEL);
  133. if (!ctrls)
  134. return -ENOMEM;
  135. v4l2_ctrl_handler_init(hdl, 6);
  136. ctrls->autogain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
  137. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  138. ctrls->exposure = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
  139. V4L2_CID_EXPOSURE, 0, 511, 1, 12);
  140. ctrls->gain = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
  141. V4L2_CID_GAIN, 0, 255, 1, 128);
  142. ctrls->red = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
  143. V4L2_CID_RED_BALANCE, -255, 255, 1, 0);
  144. ctrls->blue = v4l2_ctrl_new_std(hdl, &pb0100_ctrl_ops,
  145. V4L2_CID_BLUE_BALANCE, -255, 255, 1, 0);
  146. ctrls->natural = v4l2_ctrl_new_custom(hdl, &natural_light, NULL);
  147. ctrls->target = v4l2_ctrl_new_custom(hdl, &autogain_target, NULL);
  148. if (hdl->error) {
  149. kfree(ctrls);
  150. return hdl->error;
  151. }
  152. sd->sensor_priv = ctrls;
  153. v4l2_ctrl_auto_cluster(5, &ctrls->autogain, 0, false);
  154. return 0;
  155. }
  156. static int pb0100_probe(struct sd *sd)
  157. {
  158. u16 sensor;
  159. int err;
  160. err = stv06xx_read_sensor(sd, PB_IDENT, &sensor);
  161. if (err < 0)
  162. return -ENODEV;
  163. if ((sensor >> 8) != 0x64)
  164. return -ENODEV;
  165. pr_info("Photobit pb0100 sensor detected\n");
  166. sd->gspca_dev.cam.cam_mode = pb0100_mode;
  167. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(pb0100_mode);
  168. return 0;
  169. }
  170. static int pb0100_start(struct sd *sd)
  171. {
  172. int err, packet_size, max_packet_size;
  173. struct usb_host_interface *alt;
  174. struct usb_interface *intf;
  175. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  176. struct cam *cam = &sd->gspca_dev.cam;
  177. u32 mode = cam->cam_mode[sd->gspca_dev.curr_mode].priv;
  178. intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
  179. alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
  180. if (!alt)
  181. return -ENODEV;
  182. packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
  183. /* If we don't have enough bandwidth use a lower framerate */
  184. max_packet_size = sd->sensor->max_packet_size[sd->gspca_dev.curr_mode];
  185. if (packet_size < max_packet_size)
  186. stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
  187. else
  188. stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(5)|BIT(3)|BIT(1));
  189. /* Setup sensor window */
  190. if (mode & PB0100_CROP_TO_VGA) {
  191. stv06xx_write_sensor(sd, PB_RSTART, 30);
  192. stv06xx_write_sensor(sd, PB_CSTART, 20);
  193. stv06xx_write_sensor(sd, PB_RWSIZE, 240 - 1);
  194. stv06xx_write_sensor(sd, PB_CWSIZE, 320 - 1);
  195. } else {
  196. stv06xx_write_sensor(sd, PB_RSTART, 8);
  197. stv06xx_write_sensor(sd, PB_CSTART, 4);
  198. stv06xx_write_sensor(sd, PB_RWSIZE, 288 - 1);
  199. stv06xx_write_sensor(sd, PB_CWSIZE, 352 - 1);
  200. }
  201. if (mode & PB0100_SUBSAMPLE) {
  202. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x02); /* Wrong, FIXME */
  203. stv06xx_write_bridge(sd, STV_X_CTRL, 0x06);
  204. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x10);
  205. } else {
  206. stv06xx_write_bridge(sd, STV_Y_CTRL, 0x01);
  207. stv06xx_write_bridge(sd, STV_X_CTRL, 0x0a);
  208. /* larger -> slower */
  209. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0x20);
  210. }
  211. err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3)|BIT(1));
  212. PDEBUG(D_STREAM, "Started stream, status: %d", err);
  213. return (err < 0) ? err : 0;
  214. }
  215. static int pb0100_stop(struct sd *sd)
  216. {
  217. struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
  218. int err;
  219. err = stv06xx_write_sensor(sd, PB_ABORTFRAME, 1);
  220. if (err < 0)
  221. goto out;
  222. /* Set bit 1 to zero */
  223. err = stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
  224. PDEBUG(D_STREAM, "Halting stream");
  225. out:
  226. return (err < 0) ? err : 0;
  227. }
  228. /* FIXME: Sort the init commands out and put them into tables,
  229. this is only for getting the camera to work */
  230. /* FIXME: No error handling for now,
  231. add this once the init has been converted to proper tables */
  232. static int pb0100_init(struct sd *sd)
  233. {
  234. stv06xx_write_bridge(sd, STV_REG00, 1);
  235. stv06xx_write_bridge(sd, STV_SCAN_RATE, 0);
  236. /* Reset sensor */
  237. stv06xx_write_sensor(sd, PB_RESET, 1);
  238. stv06xx_write_sensor(sd, PB_RESET, 0);
  239. /* Disable chip */
  240. stv06xx_write_sensor(sd, PB_CONTROL, BIT(5)|BIT(3));
  241. /* Gain stuff...*/
  242. stv06xx_write_sensor(sd, PB_PREADCTRL, BIT(12)|BIT(10)|BIT(6));
  243. stv06xx_write_sensor(sd, PB_ADCGLOBALGAIN, 12);
  244. /* Set up auto-exposure */
  245. /* ADC VREF_HI new setting for a transition
  246. from the Expose1 to the Expose2 setting */
  247. stv06xx_write_sensor(sd, PB_R28, 12);
  248. /* gain max for autoexposure */
  249. stv06xx_write_sensor(sd, PB_ADCMAXGAIN, 180);
  250. /* gain min for autoexposure */
  251. stv06xx_write_sensor(sd, PB_ADCMINGAIN, 12);
  252. /* Maximum frame integration time (programmed into R8)
  253. allowed for auto-exposure routine */
  254. stv06xx_write_sensor(sd, PB_R54, 3);
  255. /* Minimum frame integration time (programmed into R8)
  256. allowed for auto-exposure routine */
  257. stv06xx_write_sensor(sd, PB_R55, 0);
  258. stv06xx_write_sensor(sd, PB_UPDATEINT, 1);
  259. /* R15 Expose0 (maximum that auto-exposure may use) */
  260. stv06xx_write_sensor(sd, PB_R15, 800);
  261. /* R17 Expose2 (minimum that auto-exposure may use) */
  262. stv06xx_write_sensor(sd, PB_R17, 10);
  263. stv06xx_write_sensor(sd, PB_EXPGAIN, 0);
  264. /* 0x14 */
  265. stv06xx_write_sensor(sd, PB_VOFFSET, 0);
  266. /* 0x0D */
  267. stv06xx_write_sensor(sd, PB_ADCGAINH, 11);
  268. /* Set black level (important!) */
  269. stv06xx_write_sensor(sd, PB_ADCGAINL, 0);
  270. /* ??? */
  271. stv06xx_write_bridge(sd, STV_REG00, 0x11);
  272. stv06xx_write_bridge(sd, STV_REG03, 0x45);
  273. stv06xx_write_bridge(sd, STV_REG04, 0x07);
  274. /* Scan/timing for the sensor */
  275. stv06xx_write_sensor(sd, PB_ROWSPEED, BIT(4)|BIT(3)|BIT(1));
  276. stv06xx_write_sensor(sd, PB_CFILLIN, 14);
  277. stv06xx_write_sensor(sd, PB_VBL, 0);
  278. stv06xx_write_sensor(sd, PB_FINTTIME, 0);
  279. stv06xx_write_sensor(sd, PB_RINTTIME, 123);
  280. stv06xx_write_bridge(sd, STV_REG01, 0xc2);
  281. stv06xx_write_bridge(sd, STV_REG02, 0xb0);
  282. return 0;
  283. }
  284. static int pb0100_dump(struct sd *sd)
  285. {
  286. return 0;
  287. }
  288. static int pb0100_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  289. {
  290. int err;
  291. struct sd *sd = (struct sd *) gspca_dev;
  292. struct pb0100_ctrls *ctrls = sd->sensor_priv;
  293. err = stv06xx_write_sensor(sd, PB_G1GAIN, val);
  294. if (!err)
  295. err = stv06xx_write_sensor(sd, PB_G2GAIN, val);
  296. PDEBUG(D_CONF, "Set green gain to %d, status: %d", val, err);
  297. if (!err)
  298. err = pb0100_set_red_balance(gspca_dev, ctrls->red->val);
  299. if (!err)
  300. err = pb0100_set_blue_balance(gspca_dev, ctrls->blue->val);
  301. return err;
  302. }
  303. static int pb0100_set_red_balance(struct gspca_dev *gspca_dev, __s32 val)
  304. {
  305. int err;
  306. struct sd *sd = (struct sd *) gspca_dev;
  307. struct pb0100_ctrls *ctrls = sd->sensor_priv;
  308. val += ctrls->gain->val;
  309. if (val < 0)
  310. val = 0;
  311. else if (val > 255)
  312. val = 255;
  313. err = stv06xx_write_sensor(sd, PB_RGAIN, val);
  314. PDEBUG(D_CONF, "Set red gain to %d, status: %d", val, err);
  315. return err;
  316. }
  317. static int pb0100_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val)
  318. {
  319. int err;
  320. struct sd *sd = (struct sd *) gspca_dev;
  321. struct pb0100_ctrls *ctrls = sd->sensor_priv;
  322. val += ctrls->gain->val;
  323. if (val < 0)
  324. val = 0;
  325. else if (val > 255)
  326. val = 255;
  327. err = stv06xx_write_sensor(sd, PB_BGAIN, val);
  328. PDEBUG(D_CONF, "Set blue gain to %d, status: %d", val, err);
  329. return err;
  330. }
  331. static int pb0100_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  332. {
  333. struct sd *sd = (struct sd *) gspca_dev;
  334. int err;
  335. err = stv06xx_write_sensor(sd, PB_RINTTIME, val);
  336. PDEBUG(D_CONF, "Set exposure to %d, status: %d", val, err);
  337. return err;
  338. }
  339. static int pb0100_set_autogain(struct gspca_dev *gspca_dev, __s32 val)
  340. {
  341. int err;
  342. struct sd *sd = (struct sd *) gspca_dev;
  343. struct pb0100_ctrls *ctrls = sd->sensor_priv;
  344. if (val) {
  345. if (ctrls->natural->val)
  346. val = BIT(6)|BIT(4)|BIT(0);
  347. else
  348. val = BIT(4)|BIT(0);
  349. } else
  350. val = 0;
  351. err = stv06xx_write_sensor(sd, PB_EXPGAIN, val);
  352. PDEBUG(D_CONF, "Set autogain to %d (natural: %d), status: %d",
  353. val, ctrls->natural->val, err);
  354. return err;
  355. }
  356. static int pb0100_set_autogain_target(struct gspca_dev *gspca_dev, __s32 val)
  357. {
  358. int err, totalpixels, brightpixels, darkpixels;
  359. struct sd *sd = (struct sd *) gspca_dev;
  360. /* Number of pixels counted by the sensor when subsampling the pixels.
  361. * Slightly larger than the real value to avoid oscillation */
  362. totalpixels = gspca_dev->pixfmt.width * gspca_dev->pixfmt.height;
  363. totalpixels = totalpixels/(8*8) + totalpixels/(64*64);
  364. brightpixels = (totalpixels * val) >> 8;
  365. darkpixels = totalpixels - brightpixels;
  366. err = stv06xx_write_sensor(sd, PB_R21, brightpixels);
  367. if (!err)
  368. err = stv06xx_write_sensor(sd, PB_R22, darkpixels);
  369. PDEBUG(D_CONF, "Set autogain target to %d, status: %d", val, err);
  370. return err;
  371. }