stv06xx_hdcs.c 12 KB

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