stv06xx_hdcs.c 13 KB

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