stv06xx_hdcs.c 14 KB

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