stv06xx_hdcs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. * Copyright (c) 2008 Chia-I Wu
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * P/N 861037: Sensor HDCS1000 ASIC STV0600
  23. * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
  24. * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
  25. * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
  26. * P/N 861075-0040: Sensor HDCS1000 ASIC
  27. * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
  28. * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
  29. */
  30. #include "stv06xx_hdcs.h"
  31. static const struct ctrl hdcs1x00_ctrl[] = {
  32. {
  33. {
  34. .id = V4L2_CID_EXPOSURE,
  35. .type = V4L2_CTRL_TYPE_INTEGER,
  36. .name = "exposure",
  37. .minimum = 0x00,
  38. .maximum = 0xff,
  39. .step = 0x1,
  40. .default_value = HDCS_DEFAULT_EXPOSURE,
  41. .flags = V4L2_CTRL_FLAG_SLIDER
  42. },
  43. .set = hdcs_set_exposure,
  44. .get = hdcs_get_exposure
  45. }, {
  46. {
  47. .id = V4L2_CID_GAIN,
  48. .type = V4L2_CTRL_TYPE_INTEGER,
  49. .name = "gain",
  50. .minimum = 0x00,
  51. .maximum = 0xff,
  52. .step = 0x1,
  53. .default_value = HDCS_DEFAULT_GAIN,
  54. .flags = V4L2_CTRL_FLAG_SLIDER
  55. },
  56. .set = hdcs_set_gain,
  57. .get = hdcs_get_gain
  58. }
  59. };
  60. static struct v4l2_pix_format hdcs1x00_mode[] = {
  61. {
  62. HDCS_1X00_DEF_WIDTH,
  63. HDCS_1X00_DEF_HEIGHT,
  64. V4L2_PIX_FMT_SGRBG8,
  65. V4L2_FIELD_NONE,
  66. .sizeimage =
  67. HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,
  68. .bytesperline = HDCS_1X00_DEF_WIDTH,
  69. .colorspace = V4L2_COLORSPACE_SRGB,
  70. .priv = 1
  71. }
  72. };
  73. static const struct ctrl hdcs1020_ctrl[] = {
  74. {
  75. {
  76. .id = V4L2_CID_EXPOSURE,
  77. .type = V4L2_CTRL_TYPE_INTEGER,
  78. .name = "exposure",
  79. .minimum = 0x00,
  80. .maximum = 0xffff,
  81. .step = 0x1,
  82. .default_value = HDCS_DEFAULT_EXPOSURE,
  83. .flags = V4L2_CTRL_FLAG_SLIDER
  84. },
  85. .set = hdcs_set_exposure,
  86. .get = hdcs_get_exposure
  87. }, {
  88. {
  89. .id = V4L2_CID_GAIN,
  90. .type = V4L2_CTRL_TYPE_INTEGER,
  91. .name = "gain",
  92. .minimum = 0x00,
  93. .maximum = 0xff,
  94. .step = 0x1,
  95. .default_value = HDCS_DEFAULT_GAIN,
  96. .flags = V4L2_CTRL_FLAG_SLIDER
  97. },
  98. .set = hdcs_set_gain,
  99. .get = hdcs_get_gain
  100. }
  101. };
  102. static struct v4l2_pix_format hdcs1020_mode[] = {
  103. {
  104. HDCS_1020_DEF_WIDTH,
  105. HDCS_1020_DEF_HEIGHT,
  106. V4L2_PIX_FMT_SGRBG8,
  107. V4L2_FIELD_NONE,
  108. .sizeimage =
  109. HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,
  110. .bytesperline = HDCS_1020_DEF_WIDTH,
  111. .colorspace = V4L2_COLORSPACE_SRGB,
  112. .priv = 1
  113. }
  114. };
  115. enum hdcs_power_state {
  116. HDCS_STATE_SLEEP,
  117. HDCS_STATE_IDLE,
  118. HDCS_STATE_RUN
  119. };
  120. /* no lock? */
  121. struct hdcs {
  122. enum hdcs_power_state state;
  123. int w, h;
  124. /* visible area of the sensor array */
  125. struct {
  126. int left, top;
  127. int width, height;
  128. int border;
  129. } array;
  130. struct {
  131. /* Column timing overhead */
  132. u8 cto;
  133. /* Column processing overhead */
  134. u8 cpo;
  135. /* Row sample period constant */
  136. u16 rs;
  137. /* Exposure reset duration */
  138. u16 er;
  139. } exp;
  140. int psmp;
  141. u8 exp_cache, gain_cache;
  142. };
  143. static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
  144. {
  145. u8 regs[I2C_MAX_BYTES * 2];
  146. int i;
  147. if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
  148. (reg + len > 0xff)))
  149. return -EINVAL;
  150. for (i = 0; i < len; i++) {
  151. regs[2 * i] = reg;
  152. regs[2 * i + 1] = vals[i];
  153. /* All addresses are shifted left one bit
  154. * as bit 0 toggles r/w */
  155. reg += 2;
  156. }
  157. return stv06xx_write_sensor_bytes(sd, regs, len);
  158. }
  159. static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
  160. {
  161. struct hdcs *hdcs = sd->sensor_priv;
  162. u8 val;
  163. int ret;
  164. if (hdcs->state == state)
  165. return 0;
  166. /* we need to go idle before running or sleeping */
  167. if (hdcs->state != HDCS_STATE_IDLE) {
  168. ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
  169. if (ret)
  170. return ret;
  171. }
  172. hdcs->state = HDCS_STATE_IDLE;
  173. if (state == HDCS_STATE_IDLE)
  174. return 0;
  175. switch (state) {
  176. case HDCS_STATE_SLEEP:
  177. val = HDCS_SLEEP_MODE;
  178. break;
  179. case HDCS_STATE_RUN:
  180. val = HDCS_RUN_ENABLE;
  181. break;
  182. default:
  183. return -EINVAL;
  184. }
  185. ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
  186. /* Update the state if the write succeeded */
  187. if (!ret)
  188. hdcs->state = state;
  189. return ret;
  190. }
  191. static int hdcs_reset(struct sd *sd)
  192. {
  193. struct hdcs *hdcs = sd->sensor_priv;
  194. int err;
  195. err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
  196. if (err < 0)
  197. return err;
  198. err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
  199. if (err < 0)
  200. hdcs->state = HDCS_STATE_IDLE;
  201. return err;
  202. }
  203. static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  204. {
  205. struct sd *sd = (struct sd *) gspca_dev;
  206. struct hdcs *hdcs = sd->sensor_priv;
  207. *val = hdcs->exp_cache;
  208. return 0;
  209. }
  210. static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  211. {
  212. struct sd *sd = (struct sd *) gspca_dev;
  213. struct hdcs *hdcs = sd->sensor_priv;
  214. int rowexp, srowexp;
  215. int max_srowexp;
  216. /* Column time period */
  217. int ct;
  218. /* Column processing period */
  219. int cp;
  220. /* Row processing period */
  221. int rp;
  222. /* Minimum number of column timing periods
  223. within the column processing period */
  224. int mnct;
  225. int cycles, err;
  226. u8 exp[14];
  227. val &= 0xff;
  228. hdcs->exp_cache = val;
  229. cycles = val * HDCS_CLK_FREQ_MHZ * 257;
  230. ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
  231. cp = hdcs->exp.cto + (hdcs->w * ct / 2);
  232. /* the cycles one row takes */
  233. rp = hdcs->exp.rs + cp;
  234. rowexp = cycles / rp;
  235. /* the remaining cycles */
  236. cycles -= rowexp * rp;
  237. /* calculate sub-row exposure */
  238. if (IS_1020(sd)) {
  239. /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
  240. srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
  241. mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
  242. max_srowexp = hdcs->w - mnct;
  243. } else {
  244. /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
  245. srowexp = cp - hdcs->exp.er - 6 - cycles;
  246. mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
  247. max_srowexp = cp - mnct * ct - 1;
  248. }
  249. if (srowexp < 0)
  250. srowexp = 0;
  251. else if (srowexp > max_srowexp)
  252. srowexp = max_srowexp;
  253. if (IS_1020(sd)) {
  254. exp[0] = HDCS20_CONTROL;
  255. exp[1] = 0x00; /* Stop streaming */
  256. exp[2] = HDCS_ROWEXPL;
  257. exp[3] = rowexp & 0xff;
  258. exp[4] = HDCS_ROWEXPH;
  259. exp[5] = rowexp >> 8;
  260. exp[6] = HDCS20_SROWEXP;
  261. exp[7] = (srowexp >> 2) & 0xff;
  262. exp[8] = HDCS20_ERROR;
  263. exp[9] = 0x10; /* Clear exposure error flag*/
  264. exp[10] = HDCS20_CONTROL;
  265. exp[11] = 0x04; /* Restart streaming */
  266. err = stv06xx_write_sensor_bytes(sd, exp, 6);
  267. } else {
  268. exp[0] = HDCS00_CONTROL;
  269. exp[1] = 0x00; /* Stop streaming */
  270. exp[2] = HDCS_ROWEXPL;
  271. exp[3] = rowexp & 0xff;
  272. exp[4] = HDCS_ROWEXPH;
  273. exp[5] = rowexp >> 8;
  274. exp[6] = HDCS00_SROWEXPL;
  275. exp[7] = srowexp & 0xff;
  276. exp[8] = HDCS00_SROWEXPH;
  277. exp[9] = srowexp >> 8;
  278. exp[10] = HDCS_STATUS;
  279. exp[11] = 0x10; /* Clear exposure error flag*/
  280. exp[12] = HDCS00_CONTROL;
  281. exp[13] = 0x04; /* Restart streaming */
  282. err = stv06xx_write_sensor_bytes(sd, exp, 7);
  283. if (err < 0)
  284. return err;
  285. }
  286. PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
  287. val, rowexp, srowexp);
  288. return err;
  289. }
  290. static int hdcs_set_gains(struct sd *sd, u8 g)
  291. {
  292. struct hdcs *hdcs = sd->sensor_priv;
  293. int err;
  294. u8 gains[4];
  295. hdcs->gain_cache = g;
  296. /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
  297. if (g > 127)
  298. g = 0x80 | (g / 2);
  299. gains[0] = g;
  300. gains[1] = g;
  301. gains[2] = g;
  302. gains[3] = g;
  303. err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
  304. return err;
  305. }
  306. static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  307. {
  308. struct sd *sd = (struct sd *) gspca_dev;
  309. struct hdcs *hdcs = sd->sensor_priv;
  310. *val = hdcs->gain_cache;
  311. return 0;
  312. }
  313. static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  314. {
  315. PDEBUG(D_V4L2, "Writing gain %d", val);
  316. return hdcs_set_gains((struct sd *) gspca_dev,
  317. val & 0xff);
  318. }
  319. static int hdcs_set_size(struct sd *sd,
  320. unsigned int width, unsigned int height)
  321. {
  322. struct hdcs *hdcs = sd->sensor_priv;
  323. u8 win[4];
  324. unsigned int x, y;
  325. int err;
  326. /* must be multiple of 4 */
  327. width = (width + 3) & ~0x3;
  328. height = (height + 3) & ~0x3;
  329. if (width > hdcs->array.width)
  330. width = hdcs->array.width;
  331. if (IS_1020(sd)) {
  332. /* the borders are also invalid */
  333. if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
  334. > hdcs->array.height)
  335. height = hdcs->array.height - 2 * hdcs->array.border -
  336. HDCS_1020_BOTTOM_Y_SKIP;
  337. y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
  338. + hdcs->array.top;
  339. } else {
  340. if (height > hdcs->array.height)
  341. height = hdcs->array.height;
  342. y = hdcs->array.top + (hdcs->array.height - height) / 2;
  343. }
  344. x = hdcs->array.left + (hdcs->array.width - width) / 2;
  345. win[0] = y / 4;
  346. win[1] = x / 4;
  347. win[2] = (y + height) / 4 - 1;
  348. win[3] = (x + width) / 4 - 1;
  349. err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
  350. if (err < 0)
  351. return err;
  352. /* Update the current width and height */
  353. hdcs->w = width;
  354. hdcs->h = height;
  355. return err;
  356. }
  357. static int hdcs_probe_1x00(struct sd *sd)
  358. {
  359. struct hdcs *hdcs;
  360. u16 sensor;
  361. int ret;
  362. ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
  363. if (ret < 0 || sensor != 0x08)
  364. return -ENODEV;
  365. info("HDCS-1000/1100 sensor detected");
  366. sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
  367. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
  368. sd->desc.ctrls = hdcs1x00_ctrl;
  369. sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
  370. hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
  371. if (!hdcs)
  372. return -ENOMEM;
  373. hdcs->array.left = 8;
  374. hdcs->array.top = 8;
  375. hdcs->array.width = HDCS_1X00_DEF_WIDTH;
  376. hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
  377. hdcs->array.border = 4;
  378. hdcs->exp.cto = 4;
  379. hdcs->exp.cpo = 2;
  380. hdcs->exp.rs = 186;
  381. hdcs->exp.er = 100;
  382. /*
  383. * Frame rate on HDCS-1000 with STV600 depends on PSMP:
  384. * 4 = doesn't work at all
  385. * 5 = 7.8 fps,
  386. * 6 = 6.9 fps,
  387. * 8 = 6.3 fps,
  388. * 10 = 5.5 fps,
  389. * 15 = 4.4 fps,
  390. * 31 = 2.8 fps
  391. *
  392. * Frame rate on HDCS-1000 with STV602 depends on PSMP:
  393. * 15 = doesn't work at all
  394. * 18 = doesn't work at all
  395. * 19 = 7.3 fps
  396. * 20 = 7.4 fps
  397. * 21 = 7.4 fps
  398. * 22 = 7.4 fps
  399. * 24 = 6.3 fps
  400. * 30 = 5.4 fps
  401. */
  402. hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
  403. sd->sensor_priv = hdcs;
  404. return 0;
  405. }
  406. static int hdcs_probe_1020(struct sd *sd)
  407. {
  408. struct hdcs *hdcs;
  409. u16 sensor;
  410. int ret;
  411. ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
  412. if (ret < 0 || sensor != 0x10)
  413. return -ENODEV;
  414. info("HDCS-1020 sensor detected");
  415. sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
  416. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
  417. sd->desc.ctrls = hdcs1020_ctrl;
  418. sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
  419. hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
  420. if (!hdcs)
  421. return -ENOMEM;
  422. /*
  423. * From Andrey's test image: looks like HDCS-1020 upper-left
  424. * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
  425. * visible pixel at 375,299 (x maybe even larger?)
  426. */
  427. hdcs->array.left = 24;
  428. hdcs->array.top = 4;
  429. hdcs->array.width = HDCS_1020_DEF_WIDTH;
  430. hdcs->array.height = 304;
  431. hdcs->array.border = 4;
  432. hdcs->psmp = 6;
  433. hdcs->exp.cto = 3;
  434. hdcs->exp.cpo = 3;
  435. hdcs->exp.rs = 155;
  436. hdcs->exp.er = 96;
  437. sd->sensor_priv = hdcs;
  438. return 0;
  439. }
  440. static int hdcs_start(struct sd *sd)
  441. {
  442. PDEBUG(D_STREAM, "Starting stream");
  443. return hdcs_set_state(sd, HDCS_STATE_RUN);
  444. }
  445. static int hdcs_stop(struct sd *sd)
  446. {
  447. PDEBUG(D_STREAM, "Halting stream");
  448. return hdcs_set_state(sd, HDCS_STATE_SLEEP);
  449. }
  450. static void hdcs_disconnect(struct sd *sd)
  451. {
  452. PDEBUG(D_PROBE, "Disconnecting the sensor");
  453. kfree(sd->sensor_priv);
  454. }
  455. static int hdcs_init(struct sd *sd)
  456. {
  457. struct hdcs *hdcs = sd->sensor_priv;
  458. int i, err = 0;
  459. /* Set the STV0602AA in STV0600 emulation mode */
  460. if (sd->bridge == BRIDGE_STV602)
  461. stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
  462. /* Execute the bridge init */
  463. for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
  464. err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
  465. stv_bridge_init[i][1]);
  466. }
  467. if (err < 0)
  468. return err;
  469. /* sensor soft reset */
  470. hdcs_reset(sd);
  471. /* Execute the sensor init */
  472. for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
  473. err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
  474. stv_sensor_init[i][1]);
  475. }
  476. if (err < 0)
  477. return err;
  478. /* Enable continuous frame capture, bit 2: stop when frame complete */
  479. err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
  480. if (err < 0)
  481. return err;
  482. /* Set PGA sample duration
  483. (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
  484. if (IS_1020(sd))
  485. err = stv06xx_write_sensor(sd, HDCS_TCTRL,
  486. (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
  487. else
  488. err = stv06xx_write_sensor(sd, HDCS_TCTRL,
  489. (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
  490. if (err < 0)
  491. return err;
  492. err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN);
  493. if (err < 0)
  494. return err;
  495. err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
  496. if (err < 0)
  497. return err;
  498. err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
  499. return err;
  500. }
  501. static int hdcs_dump(struct sd *sd)
  502. {
  503. u16 reg, val;
  504. info("Dumping sensor registers:");
  505. for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
  506. stv06xx_read_sensor(sd, reg, &val);
  507. info("reg 0x%02x = 0x%02x", reg, val);
  508. }
  509. return 0;
  510. }