stv06xx_hdcs.c 13 KB

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