stv06xx_hdcs.c 14 KB

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