stv06xx_hdcs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 as bit 0 toggles r/w */
  154. reg += 2;
  155. }
  156. return stv06xx_write_sensor_bytes(sd, regs, len);
  157. }
  158. static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
  159. {
  160. struct hdcs *hdcs = sd->sensor_priv;
  161. u8 val;
  162. int ret;
  163. if (hdcs->state == state)
  164. return 0;
  165. /* we need to go idle before running or sleeping */
  166. if (hdcs->state != HDCS_STATE_IDLE) {
  167. ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
  168. if (ret)
  169. return ret;
  170. }
  171. hdcs->state = HDCS_STATE_IDLE;
  172. if (state == HDCS_STATE_IDLE)
  173. return 0;
  174. switch (state) {
  175. case HDCS_STATE_SLEEP:
  176. val = HDCS_SLEEP_MODE;
  177. break;
  178. case HDCS_STATE_RUN:
  179. val = HDCS_RUN_ENABLE;
  180. break;
  181. default:
  182. return -EINVAL;
  183. }
  184. ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
  185. /* Update the state if the write succeeded */
  186. if (!ret)
  187. hdcs->state = state;
  188. return ret;
  189. }
  190. static int hdcs_reset(struct sd *sd)
  191. {
  192. struct hdcs *hdcs = sd->sensor_priv;
  193. int err;
  194. err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
  195. if (err < 0)
  196. return err;
  197. err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
  198. if (err < 0)
  199. hdcs->state = HDCS_STATE_IDLE;
  200. return err;
  201. }
  202. static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
  203. {
  204. struct sd *sd = (struct sd *) gspca_dev;
  205. struct hdcs *hdcs = sd->sensor_priv;
  206. *val = hdcs->exp_cache;
  207. return 0;
  208. }
  209. static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
  210. {
  211. struct sd *sd = (struct sd *) gspca_dev;
  212. struct hdcs *hdcs = sd->sensor_priv;
  213. int rowexp, srowexp;
  214. int max_srowexp;
  215. /* Column time period */
  216. int ct;
  217. /* Column processing period */
  218. int cp;
  219. /* Row processing period */
  220. int rp;
  221. /* Minimum number of column timing periods
  222. within the column processing period */
  223. int mnct;
  224. int cycles, err;
  225. u8 exp[14];
  226. val &= 0xff;
  227. hdcs->exp_cache = val;
  228. cycles = val * HDCS_CLK_FREQ_MHZ * 257;
  229. ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
  230. cp = hdcs->exp.cto + (hdcs->w * ct / 2);
  231. /* the cycles one row takes */
  232. rp = hdcs->exp.rs + cp;
  233. rowexp = cycles / rp;
  234. /* the remaining cycles */
  235. cycles -= rowexp * rp;
  236. /* calculate sub-row exposure */
  237. if (IS_1020(sd)) {
  238. /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
  239. srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
  240. mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
  241. max_srowexp = hdcs->w - mnct;
  242. } else {
  243. /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
  244. srowexp = cp - hdcs->exp.er - 6 - cycles;
  245. mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
  246. max_srowexp = cp - mnct * ct - 1;
  247. }
  248. if (srowexp < 0)
  249. srowexp = 0;
  250. else if (srowexp > max_srowexp)
  251. srowexp = max_srowexp;
  252. if (IS_1020(sd)) {
  253. exp[0] = HDCS20_CONTROL;
  254. exp[1] = 0x00; /* Stop streaming */
  255. exp[2] = HDCS_ROWEXPL;
  256. exp[3] = rowexp & 0xff;
  257. exp[4] = HDCS_ROWEXPH;
  258. exp[5] = rowexp >> 8;
  259. exp[6] = HDCS20_SROWEXP;
  260. exp[7] = (srowexp >> 2) & 0xff;
  261. exp[8] = HDCS20_ERROR;
  262. exp[9] = 0x10; /* Clear exposure error flag*/
  263. exp[10] = HDCS20_CONTROL;
  264. exp[11] = 0x04; /* Restart streaming */
  265. err = stv06xx_write_sensor_bytes(sd, exp, 6);
  266. } else {
  267. exp[0] = HDCS00_CONTROL;
  268. exp[1] = 0x00; /* Stop streaming */
  269. exp[2] = HDCS_ROWEXPL;
  270. exp[3] = rowexp & 0xff;
  271. exp[4] = HDCS_ROWEXPH;
  272. exp[5] = rowexp >> 8;
  273. exp[6] = HDCS00_SROWEXPL;
  274. exp[7] = srowexp & 0xff;
  275. exp[8] = HDCS00_SROWEXPH;
  276. exp[9] = srowexp >> 8;
  277. exp[10] = HDCS_STATUS;
  278. exp[11] = 0x10; /* Clear exposure error flag*/
  279. exp[12] = HDCS00_CONTROL;
  280. exp[13] = 0x04; /* Restart streaming */
  281. err = stv06xx_write_sensor_bytes(sd, exp, 7);
  282. if (err < 0)
  283. return err;
  284. }
  285. PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
  286. val, rowexp, srowexp);
  287. return err;
  288. }
  289. static int hdcs_set_gains(struct sd *sd, u8 g)
  290. {
  291. struct hdcs *hdcs = sd->sensor_priv;
  292. int err;
  293. u8 gains[4];
  294. hdcs->gain_cache = g;
  295. /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
  296. if (g > 127)
  297. g = 0x80 | (g / 2);
  298. gains[0] = g;
  299. gains[1] = g;
  300. gains[2] = g;
  301. gains[3] = g;
  302. err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
  303. return err;
  304. }
  305. static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
  306. {
  307. struct sd *sd = (struct sd *) gspca_dev;
  308. struct hdcs *hdcs = sd->sensor_priv;
  309. *val = hdcs->gain_cache;
  310. return 0;
  311. }
  312. static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
  313. {
  314. PDEBUG(D_V4L2, "Writing gain %d", val);
  315. return hdcs_set_gains((struct sd *) gspca_dev,
  316. val & 0xff);
  317. }
  318. static int hdcs_set_size(struct sd *sd,
  319. unsigned int width, unsigned int height)
  320. {
  321. struct hdcs *hdcs = sd->sensor_priv;
  322. u8 win[4];
  323. unsigned int x, y;
  324. int err;
  325. /* must be multiple of 4 */
  326. width = (width + 3) & ~0x3;
  327. height = (height + 3) & ~0x3;
  328. if (width > hdcs->array.width)
  329. width = hdcs->array.width;
  330. if (IS_1020(sd)) {
  331. /* the borders are also invalid */
  332. if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
  333. > hdcs->array.height)
  334. height = hdcs->array.height - 2 * hdcs->array.border -
  335. HDCS_1020_BOTTOM_Y_SKIP;
  336. y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
  337. + hdcs->array.top;
  338. } else {
  339. if (height > hdcs->array.height)
  340. height = hdcs->array.height;
  341. y = hdcs->array.top + (hdcs->array.height - height) / 2;
  342. }
  343. x = hdcs->array.left + (hdcs->array.width - width) / 2;
  344. win[0] = y / 4;
  345. win[1] = x / 4;
  346. win[2] = (y + height) / 4 - 1;
  347. win[3] = (x + width) / 4 - 1;
  348. err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
  349. if (err < 0)
  350. return err;
  351. /* Update the current width and height */
  352. hdcs->w = width;
  353. hdcs->h = height;
  354. return err;
  355. }
  356. static int hdcs_probe_1x00(struct sd *sd)
  357. {
  358. struct hdcs *hdcs;
  359. u16 sensor;
  360. int ret;
  361. ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
  362. if (ret < 0 || sensor != 0x08)
  363. return -ENODEV;
  364. info("HDCS-1000/1100 sensor detected");
  365. sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
  366. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
  367. sd->desc.ctrls = hdcs1x00_ctrl;
  368. sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
  369. hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
  370. if (!hdcs)
  371. return -ENOMEM;
  372. hdcs->array.left = 8;
  373. hdcs->array.top = 8;
  374. hdcs->array.width = HDCS_1X00_DEF_WIDTH;
  375. hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
  376. hdcs->array.border = 4;
  377. hdcs->exp.cto = 4;
  378. hdcs->exp.cpo = 2;
  379. hdcs->exp.rs = 186;
  380. hdcs->exp.er = 100;
  381. /*
  382. * Frame rate on HDCS-1000 with STV600 depends on PSMP:
  383. * 4 = doesn't work at all
  384. * 5 = 7.8 fps,
  385. * 6 = 6.9 fps,
  386. * 8 = 6.3 fps,
  387. * 10 = 5.5 fps,
  388. * 15 = 4.4 fps,
  389. * 31 = 2.8 fps
  390. *
  391. * Frame rate on HDCS-1000 with STV602 depends on PSMP:
  392. * 15 = doesn't work at all
  393. * 18 = doesn't work at all
  394. * 19 = 7.3 fps
  395. * 20 = 7.4 fps
  396. * 21 = 7.4 fps
  397. * 22 = 7.4 fps
  398. * 24 = 6.3 fps
  399. * 30 = 5.4 fps
  400. */
  401. hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
  402. sd->sensor_priv = hdcs;
  403. return 0;
  404. }
  405. static int hdcs_probe_1020(struct sd *sd)
  406. {
  407. struct hdcs *hdcs;
  408. u16 sensor;
  409. int ret;
  410. ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
  411. if (ret < 0 || sensor != 0x10)
  412. return -ENODEV;
  413. info("HDCS-1020 sensor detected");
  414. sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
  415. sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
  416. sd->desc.ctrls = hdcs1020_ctrl;
  417. sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
  418. hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
  419. if (!hdcs)
  420. return -ENOMEM;
  421. /*
  422. * From Andrey's test image: looks like HDCS-1020 upper-left
  423. * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
  424. * visible pixel at 375,299 (x maybe even larger?)
  425. */
  426. hdcs->array.left = 24;
  427. hdcs->array.top = 4;
  428. hdcs->array.width = HDCS_1020_DEF_WIDTH;
  429. hdcs->array.height = 304;
  430. hdcs->array.border = 4;
  431. hdcs->psmp = 6;
  432. hdcs->exp.cto = 3;
  433. hdcs->exp.cpo = 3;
  434. hdcs->exp.rs = 155;
  435. hdcs->exp.er = 96;
  436. sd->sensor_priv = hdcs;
  437. return 0;
  438. }
  439. static int hdcs_start(struct sd *sd)
  440. {
  441. PDEBUG(D_STREAM, "Starting stream");
  442. return hdcs_set_state(sd, HDCS_STATE_RUN);
  443. }
  444. static int hdcs_stop(struct sd *sd)
  445. {
  446. PDEBUG(D_STREAM, "Halting stream");
  447. return hdcs_set_state(sd, HDCS_STATE_SLEEP);
  448. }
  449. static void hdcs_disconnect(struct sd *sd)
  450. {
  451. PDEBUG(D_PROBE, "Disconnecting the sensor");
  452. kfree(sd->sensor_priv);
  453. }
  454. static int hdcs_init(struct sd *sd)
  455. {
  456. struct hdcs *hdcs = sd->sensor_priv;
  457. int i, err = 0;
  458. /* Set the STV0602AA in STV0600 emulation mode */
  459. if (sd->bridge == BRIDGE_STV602)
  460. stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
  461. /* Execute the bridge init */
  462. for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
  463. err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
  464. stv_bridge_init[i][1]);
  465. }
  466. if (err < 0)
  467. return err;
  468. /* sensor soft reset */
  469. hdcs_reset(sd);
  470. /* Execute the sensor init */
  471. for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
  472. err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
  473. stv_sensor_init[i][1]);
  474. }
  475. if (err < 0)
  476. return err;
  477. /* Enable continous frame capture, bit 2: stop when frame complete */
  478. err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
  479. if (err < 0)
  480. return err;
  481. /* Set PGA sample duration
  482. (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
  483. if (IS_1020(sd))
  484. err = stv06xx_write_sensor(sd, HDCS_TCTRL,
  485. (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
  486. else
  487. err = stv06xx_write_sensor(sd, HDCS_TCTRL,
  488. (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
  489. if (err < 0)
  490. return err;
  491. err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN);
  492. if (err < 0)
  493. return err;
  494. err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
  495. if (err < 0)
  496. return err;
  497. err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
  498. return err;
  499. }
  500. static int hdcs_dump(struct sd *sd)
  501. {
  502. u16 reg, val;
  503. info("Dumping sensor registers:");
  504. for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
  505. stv06xx_read_sensor(sd, reg, &val);
  506. info("reg 0x%02x = 0x%02x", reg, val);
  507. }
  508. return 0;
  509. }