stk-sensor.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* stk-sensor.c: Driver for ov96xx sensor (used in some Syntek webcams)
  2. *
  3. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  4. *
  5. * Some parts derived from ov7670.c:
  6. * Copyright 2006 One Laptop Per Child Association, Inc. Written
  7. * by Jonathan Corbet with substantial inspiration from Mark
  8. * McClelland's ovcamchip code.
  9. *
  10. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  11. *
  12. * This file may be distributed under the terms of the GNU General
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. /* Controlling the sensor via the STK1125 vendor specific control interface:
  24. * The camera uses an OmniVision sensor and the stk1125 provides an
  25. * SCCB(i2c)-USB bridge which let us program the sensor.
  26. * In my case the sensor id is 0x9652, it can be read from sensor's register
  27. * 0x0A and 0x0B as follows:
  28. * - read register #R:
  29. * output #R to index 0x0208
  30. * output 0x0070 to index 0x0200
  31. * input 1 byte from index 0x0201 (some kind of status register)
  32. * until its value is 0x01
  33. * input 1 byte from index 0x0209. This is the value of #R
  34. * - write value V to register #R
  35. * output #R to index 0x0204
  36. * output V to index 0x0205
  37. * output 0x0005 to index 0x0200
  38. * input 1 byte from index 0x0201 until its value becomes 0x04
  39. */
  40. /* It seems the i2c bus is controlled with these registers */
  41. #include "stk-webcam.h"
  42. #define STK_IIC_BASE (0x0200)
  43. # define STK_IIC_OP (STK_IIC_BASE)
  44. # define STK_IIC_OP_TX (0x05)
  45. # define STK_IIC_OP_RX (0x70)
  46. # define STK_IIC_STAT (STK_IIC_BASE+1)
  47. # define STK_IIC_STAT_TX_OK (0x04)
  48. # define STK_IIC_STAT_RX_OK (0x01)
  49. /* I don't know what does this register.
  50. * when it is 0x00 or 0x01, we cannot talk to the sensor,
  51. * other values work */
  52. # define STK_IIC_ENABLE (STK_IIC_BASE+2)
  53. # define STK_IIC_ENABLE_NO (0x00)
  54. /* This is what the driver writes in windows */
  55. # define STK_IIC_ENABLE_YES (0x1e)
  56. /*
  57. * Address of the slave. Seems like the binary driver look for the
  58. * sensor in multiple places, attempting a reset sequence.
  59. * We only know about the ov9650
  60. */
  61. # define STK_IIC_ADDR (STK_IIC_BASE+3)
  62. # define STK_IIC_TX_INDEX (STK_IIC_BASE+4)
  63. # define STK_IIC_TX_VALUE (STK_IIC_BASE+5)
  64. # define STK_IIC_RX_INDEX (STK_IIC_BASE+8)
  65. # define STK_IIC_RX_VALUE (STK_IIC_BASE+9)
  66. #define MAX_RETRIES (50)
  67. #define SENSOR_ADDRESS (0x60)
  68. /* From ov7670.c (These registers aren't fully accurate) */
  69. /* Registers */
  70. #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
  71. #define REG_BLUE 0x01 /* blue gain */
  72. #define REG_RED 0x02 /* red gain */
  73. #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
  74. #define REG_COM1 0x04 /* Control 1 */
  75. #define COM1_CCIR656 0x40 /* CCIR656 enable */
  76. #define COM1_QFMT 0x20 /* QVGA/QCIF format */
  77. #define COM1_SKIP_0 0x00 /* Do not skip any row */
  78. #define COM1_SKIP_2 0x04 /* Skip 2 rows of 4 */
  79. #define COM1_SKIP_3 0x08 /* Skip 3 rows of 4 */
  80. #define REG_BAVE 0x05 /* U/B Average level */
  81. #define REG_GbAVE 0x06 /* Y/Gb Average level */
  82. #define REG_AECHH 0x07 /* AEC MS 5 bits */
  83. #define REG_RAVE 0x08 /* V/R Average level */
  84. #define REG_COM2 0x09 /* Control 2 */
  85. #define COM2_SSLEEP 0x10 /* Soft sleep mode */
  86. #define REG_PID 0x0a /* Product ID MSB */
  87. #define REG_VER 0x0b /* Product ID LSB */
  88. #define REG_COM3 0x0c /* Control 3 */
  89. #define COM3_SWAP 0x40 /* Byte swap */
  90. #define COM3_SCALEEN 0x08 /* Enable scaling */
  91. #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
  92. #define REG_COM4 0x0d /* Control 4 */
  93. #define REG_COM5 0x0e /* All "reserved" */
  94. #define REG_COM6 0x0f /* Control 6 */
  95. #define REG_AECH 0x10 /* More bits of AEC value */
  96. #define REG_CLKRC 0x11 /* Clock control */
  97. #define CLK_PLL 0x80 /* Enable internal PLL */
  98. #define CLK_EXT 0x40 /* Use external clock directly */
  99. #define CLK_SCALE 0x3f /* Mask for internal clock scale */
  100. #define REG_COM7 0x12 /* Control 7 */
  101. #define COM7_RESET 0x80 /* Register reset */
  102. #define COM7_FMT_MASK 0x38
  103. #define COM7_FMT_SXGA 0x00
  104. #define COM7_FMT_VGA 0x40
  105. #define COM7_FMT_CIF 0x20 /* CIF format */
  106. #define COM7_FMT_QVGA 0x10 /* QVGA format */
  107. #define COM7_FMT_QCIF 0x08 /* QCIF format */
  108. #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
  109. #define COM7_YUV 0x00 /* YUV */
  110. #define COM7_BAYER 0x01 /* Bayer format */
  111. #define COM7_PBAYER 0x05 /* "Processed bayer" */
  112. #define REG_COM8 0x13 /* Control 8 */
  113. #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
  114. #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
  115. #define COM8_BFILT 0x20 /* Band filter enable */
  116. #define COM8_AGC 0x04 /* Auto gain enable */
  117. #define COM8_AWB 0x02 /* White balance enable */
  118. #define COM8_AEC 0x01 /* Auto exposure enable */
  119. #define REG_COM9 0x14 /* Control 9 - gain ceiling */
  120. #define REG_COM10 0x15 /* Control 10 */
  121. #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
  122. #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
  123. #define COM10_HREF_REV 0x08 /* Reverse HREF */
  124. #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
  125. #define COM10_VS_NEG 0x02 /* VSYNC negative */
  126. #define COM10_HS_NEG 0x01 /* HSYNC negative */
  127. #define REG_HSTART 0x17 /* Horiz start high bits */
  128. #define REG_HSTOP 0x18 /* Horiz stop high bits */
  129. #define REG_VSTART 0x19 /* Vert start high bits */
  130. #define REG_VSTOP 0x1a /* Vert stop high bits */
  131. #define REG_PSHFT 0x1b /* Pixel delay after HREF */
  132. #define REG_MIDH 0x1c /* Manuf. ID high */
  133. #define REG_MIDL 0x1d /* Manuf. ID low */
  134. #define REG_MVFP 0x1e /* Mirror / vflip */
  135. #define MVFP_MIRROR 0x20 /* Mirror image */
  136. #define MVFP_FLIP 0x10 /* Vertical flip */
  137. #define REG_AEW 0x24 /* AGC upper limit */
  138. #define REG_AEB 0x25 /* AGC lower limit */
  139. #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
  140. #define REG_ADVFL 0x2d /* Insert dummy lines (LSB) */
  141. #define REG_ADVFH 0x2e /* Insert dummy lines (MSB) */
  142. #define REG_HSYST 0x30 /* HSYNC rising edge delay */
  143. #define REG_HSYEN 0x31 /* HSYNC falling edge delay */
  144. #define REG_HREF 0x32 /* HREF pieces */
  145. #define REG_TSLB 0x3a /* lots of stuff */
  146. #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
  147. #define TSLB_BYTEORD 0x08 /* swap bytes in 16bit mode? */
  148. #define REG_COM11 0x3b /* Control 11 */
  149. #define COM11_NIGHT 0x80 /* NIght mode enable */
  150. #define COM11_NMFR 0x60 /* Two bit NM frame rate */
  151. #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
  152. #define COM11_50HZ 0x08 /* Manual 50Hz select */
  153. #define COM11_EXP 0x02
  154. #define REG_COM12 0x3c /* Control 12 */
  155. #define COM12_HREF 0x80 /* HREF always */
  156. #define REG_COM13 0x3d /* Control 13 */
  157. #define COM13_GAMMA 0x80 /* Gamma enable */
  158. #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
  159. #define COM13_CMATRIX 0x10 /* Enable color matrix for RGB or YUV */
  160. #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
  161. #define REG_COM14 0x3e /* Control 14 */
  162. #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
  163. #define REG_EDGE 0x3f /* Edge enhancement factor */
  164. #define REG_COM15 0x40 /* Control 15 */
  165. #define COM15_R10F0 0x00 /* Data range 10 to F0 */
  166. #define COM15_R01FE 0x80 /* 01 to FE */
  167. #define COM15_R00FF 0xc0 /* 00 to FF */
  168. #define COM15_RGB565 0x10 /* RGB565 output */
  169. #define COM15_RGBFIXME 0x20 /* FIXME */
  170. #define COM15_RGB555 0x30 /* RGB555 output */
  171. #define REG_COM16 0x41 /* Control 16 */
  172. #define COM16_AWBGAIN 0x08 /* AWB gain enable */
  173. #define REG_COM17 0x42 /* Control 17 */
  174. #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
  175. #define COM17_CBAR 0x08 /* DSP Color bar */
  176. /*
  177. * This matrix defines how the colors are generated, must be
  178. * tweaked to adjust hue and saturation.
  179. *
  180. * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
  181. *
  182. * They are nine-bit signed quantities, with the sign bit
  183. * stored in 0x58. Sign for v-red is bit 0, and up from there.
  184. */
  185. #define REG_CMATRIX_BASE 0x4f
  186. #define CMATRIX_LEN 6
  187. #define REG_CMATRIX_SIGN 0x58
  188. #define REG_BRIGHT 0x55 /* Brightness */
  189. #define REG_CONTRAS 0x56 /* Contrast control */
  190. #define REG_GFIX 0x69 /* Fix gain control */
  191. #define REG_RGB444 0x8c /* RGB 444 control */
  192. #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
  193. #define R444_RGBX 0x01 /* Empty nibble at end */
  194. #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
  195. #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
  196. #define REG_BD50MAX 0xa5 /* 50hz banding step limit */
  197. #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
  198. #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
  199. #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
  200. #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
  201. #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
  202. #define REG_BD60MAX 0xab /* 60hz banding step limit */
  203. /* Returns 0 if OK */
  204. static int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val)
  205. {
  206. int i = 0;
  207. u8 tmpval = 0;
  208. if (stk_camera_write_reg(dev, STK_IIC_TX_INDEX, reg))
  209. return 1;
  210. if (stk_camera_write_reg(dev, STK_IIC_TX_VALUE, val))
  211. return 1;
  212. if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_TX))
  213. return 1;
  214. do {
  215. if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
  216. return 1;
  217. i++;
  218. } while (tmpval == 0 && i < MAX_RETRIES);
  219. if (tmpval != STK_IIC_STAT_TX_OK) {
  220. if (tmpval)
  221. STK_ERROR("stk_sensor_outb failed, status=0x%02x\n",
  222. tmpval);
  223. return 1;
  224. } else
  225. return 0;
  226. }
  227. static int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val)
  228. {
  229. int i = 0;
  230. u8 tmpval = 0;
  231. if (stk_camera_write_reg(dev, STK_IIC_RX_INDEX, reg))
  232. return 1;
  233. if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_RX))
  234. return 1;
  235. do {
  236. if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
  237. return 1;
  238. i++;
  239. } while (tmpval == 0 && i < MAX_RETRIES);
  240. if (tmpval != STK_IIC_STAT_RX_OK) {
  241. if (tmpval)
  242. STK_ERROR("stk_sensor_inb failed, status=0x%02x\n",
  243. tmpval);
  244. return 1;
  245. }
  246. if (stk_camera_read_reg(dev, STK_IIC_RX_VALUE, &tmpval))
  247. return 1;
  248. *val = tmpval;
  249. return 0;
  250. }
  251. static int stk_sensor_write_regvals(struct stk_camera *dev,
  252. struct regval *rv)
  253. {
  254. int ret;
  255. if (rv == NULL)
  256. return 0;
  257. while (rv->reg != 0xff || rv->val != 0xff) {
  258. ret = stk_sensor_outb(dev, rv->reg, rv->val);
  259. if (ret != 0)
  260. return ret;
  261. rv++;
  262. }
  263. return 0;
  264. }
  265. int stk_sensor_sleep(struct stk_camera *dev)
  266. {
  267. u8 tmp;
  268. return stk_sensor_inb(dev, REG_COM2, &tmp)
  269. || stk_sensor_outb(dev, REG_COM2, tmp|COM2_SSLEEP);
  270. }
  271. int stk_sensor_wakeup(struct stk_camera *dev)
  272. {
  273. u8 tmp;
  274. return stk_sensor_inb(dev, REG_COM2, &tmp)
  275. || stk_sensor_outb(dev, REG_COM2, tmp&~COM2_SSLEEP);
  276. }
  277. static struct regval ov_initvals[] = {
  278. {REG_CLKRC, CLK_PLL},
  279. {REG_COM11, 0x01},
  280. {0x6a, 0x7d},
  281. {REG_AECH, 0x40},
  282. {REG_GAIN, 0x00},
  283. {REG_BLUE, 0x80},
  284. {REG_RED, 0x80},
  285. /* Do not enable fast AEC for now */
  286. /*{REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},*/
  287. {REG_COM8, COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},
  288. {0x39, 0x50}, {0x38, 0x93},
  289. {0x37, 0x00}, {0x35, 0x81},
  290. {REG_COM5, 0x20},
  291. {REG_COM1, 0x00},
  292. {REG_COM3, 0x00},
  293. {REG_COM4, 0x00},
  294. {REG_PSHFT, 0x00},
  295. {0x16, 0x07},
  296. {0x33, 0xe2}, {0x34, 0xbf},
  297. {REG_COM16, 0x00},
  298. {0x96, 0x04},
  299. /* Gamma curve values */
  300. /* { 0x7a, 0x20 }, { 0x7b, 0x10 },
  301. { 0x7c, 0x1e }, { 0x7d, 0x35 },
  302. { 0x7e, 0x5a }, { 0x7f, 0x69 },
  303. { 0x80, 0x76 }, { 0x81, 0x80 },
  304. { 0x82, 0x88 }, { 0x83, 0x8f },
  305. { 0x84, 0x96 }, { 0x85, 0xa3 },
  306. { 0x86, 0xaf }, { 0x87, 0xc4 },
  307. { 0x88, 0xd7 }, { 0x89, 0xe8 },
  308. */
  309. {REG_GFIX, 0x40},
  310. {0x8e, 0x00},
  311. {REG_COM12, 0x73},
  312. {0x8f, 0xdf}, {0x8b, 0x06},
  313. {0x8c, 0x20},
  314. {0x94, 0x88}, {0x95, 0x88},
  315. /* {REG_COM15, 0xc1}, TODO */
  316. {0x29, 0x3f},
  317. {REG_COM6, 0x42},
  318. {REG_BD50MAX, 0x80},
  319. {REG_HAECC6, 0xb8}, {REG_HAECC7, 0x92},
  320. {REG_BD60MAX, 0x0a},
  321. {0x90, 0x00}, {0x91, 0x00},
  322. {REG_HAECC1, 0x00}, {REG_HAECC2, 0x00},
  323. {REG_AEW, 0x68}, {REG_AEB, 0x5c},
  324. {REG_VPT, 0xc3},
  325. {REG_COM9, 0x2e},
  326. {0x2a, 0x00}, {0x2b, 0x00},
  327. {0xff, 0xff}, /* END MARKER */
  328. };
  329. /* Probe the I2C bus and initialise the sensor chip */
  330. int stk_sensor_init(struct stk_camera *dev)
  331. {
  332. u8 idl = 0;
  333. u8 idh = 0;
  334. if (stk_camera_write_reg(dev, STK_IIC_ENABLE, STK_IIC_ENABLE_YES)
  335. || stk_camera_write_reg(dev, STK_IIC_ADDR, SENSOR_ADDRESS)
  336. || stk_sensor_outb(dev, REG_COM7, COM7_RESET)) {
  337. STK_ERROR("Sensor resetting failed\n");
  338. return -ENODEV;
  339. }
  340. msleep(10);
  341. /* Read the manufacturer ID: ov = 0x7FA2 */
  342. if (stk_sensor_inb(dev, REG_MIDH, &idh)
  343. || stk_sensor_inb(dev, REG_MIDL, &idl)) {
  344. STK_ERROR("Strange error reading sensor ID\n");
  345. return -ENODEV;
  346. }
  347. if (idh != 0x7f || idl != 0xa2) {
  348. STK_ERROR("Huh? you don't have a sensor from ovt\n");
  349. return -ENODEV;
  350. }
  351. if (stk_sensor_inb(dev, REG_PID, &idh)
  352. || stk_sensor_inb(dev, REG_VER, &idl)) {
  353. STK_ERROR("Could not read sensor model\n");
  354. return -ENODEV;
  355. }
  356. stk_sensor_write_regvals(dev, ov_initvals);
  357. msleep(10);
  358. STK_INFO("OmniVision sensor detected, id %02X%02X at address %x\n",
  359. idh, idl, SENSOR_ADDRESS);
  360. return 0;
  361. }
  362. /* V4L2_PIX_FMT_UYVY */
  363. static struct regval ov_fmt_uyvy[] = {
  364. {REG_TSLB, TSLB_YLAST|0x08 },
  365. { 0x4f, 0x80 }, /* "matrix coefficient 1" */
  366. { 0x50, 0x80 }, /* "matrix coefficient 2" */
  367. { 0x51, 0 }, /* vb */
  368. { 0x52, 0x22 }, /* "matrix coefficient 4" */
  369. { 0x53, 0x5e }, /* "matrix coefficient 5" */
  370. { 0x54, 0x80 }, /* "matrix coefficient 6" */
  371. {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
  372. {REG_COM15, COM15_R00FF },
  373. {0xff, 0xff}, /* END MARKER */
  374. };
  375. /* V4L2_PIX_FMT_YUYV */
  376. static struct regval ov_fmt_yuyv[] = {
  377. {REG_TSLB, 0 },
  378. { 0x4f, 0x80 }, /* "matrix coefficient 1" */
  379. { 0x50, 0x80 }, /* "matrix coefficient 2" */
  380. { 0x51, 0 }, /* vb */
  381. { 0x52, 0x22 }, /* "matrix coefficient 4" */
  382. { 0x53, 0x5e }, /* "matrix coefficient 5" */
  383. { 0x54, 0x80 }, /* "matrix coefficient 6" */
  384. {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
  385. {REG_COM15, COM15_R00FF },
  386. {0xff, 0xff}, /* END MARKER */
  387. };
  388. /* V4L2_PIX_FMT_RGB565X rrrrrggg gggbbbbb */
  389. static struct regval ov_fmt_rgbr[] = {
  390. { REG_RGB444, 0 }, /* No RGB444 please */
  391. {REG_TSLB, 0x00},
  392. { REG_COM1, 0x0 },
  393. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  394. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  395. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  396. { 0x51, 0 }, /* vb */
  397. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  398. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  399. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  400. { REG_COM13, COM13_GAMMA },
  401. { REG_COM15, COM15_RGB565|COM15_R00FF },
  402. { 0xff, 0xff },
  403. };
  404. /* V4L2_PIX_FMT_RGB565 gggbbbbb rrrrrggg */
  405. static struct regval ov_fmt_rgbp[] = {
  406. { REG_RGB444, 0 }, /* No RGB444 please */
  407. {REG_TSLB, TSLB_BYTEORD },
  408. { REG_COM1, 0x0 },
  409. { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
  410. { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
  411. { 0x50, 0xb3 }, /* "matrix coefficient 2" */
  412. { 0x51, 0 }, /* vb */
  413. { 0x52, 0x3d }, /* "matrix coefficient 4" */
  414. { 0x53, 0xa7 }, /* "matrix coefficient 5" */
  415. { 0x54, 0xe4 }, /* "matrix coefficient 6" */
  416. { REG_COM13, COM13_GAMMA },
  417. { REG_COM15, COM15_RGB565|COM15_R00FF },
  418. { 0xff, 0xff },
  419. };
  420. /* V4L2_PIX_FMT_SRGGB8 */
  421. static struct regval ov_fmt_bayer[] = {
  422. /* This changes color order */
  423. {REG_TSLB, 0x40}, /* BGGR */
  424. /* {REG_TSLB, 0x08}, */ /* BGGR with vertical image flipping */
  425. {REG_COM15, COM15_R00FF },
  426. {0xff, 0xff}, /* END MARKER */
  427. };
  428. /*
  429. * Store a set of start/stop values into the camera.
  430. */
  431. static int stk_sensor_set_hw(struct stk_camera *dev,
  432. int hstart, int hstop, int vstart, int vstop)
  433. {
  434. int ret;
  435. unsigned char v;
  436. /*
  437. * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
  438. * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
  439. * a mystery "edge offset" value in the top two bits of href.
  440. */
  441. ret = stk_sensor_outb(dev, REG_HSTART, (hstart >> 3) & 0xff);
  442. ret += stk_sensor_outb(dev, REG_HSTOP, (hstop >> 3) & 0xff);
  443. ret += stk_sensor_inb(dev, REG_HREF, &v);
  444. v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
  445. msleep(10);
  446. ret += stk_sensor_outb(dev, REG_HREF, v);
  447. /*
  448. * Vertical: similar arrangement (note: this is different from ov7670.c)
  449. */
  450. ret += stk_sensor_outb(dev, REG_VSTART, (vstart >> 3) & 0xff);
  451. ret += stk_sensor_outb(dev, REG_VSTOP, (vstop >> 3) & 0xff);
  452. ret += stk_sensor_inb(dev, REG_VREF, &v);
  453. v = (v & 0xc0) | ((vstop & 0x7) << 3) | (vstart & 0x7);
  454. msleep(10);
  455. ret += stk_sensor_outb(dev, REG_VREF, v);
  456. return ret;
  457. }
  458. int stk_sensor_configure(struct stk_camera *dev)
  459. {
  460. int com7;
  461. /*
  462. * We setup the sensor to output dummy lines in low-res modes,
  463. * so we don't get absurdly hight framerates.
  464. */
  465. unsigned dummylines;
  466. int flip;
  467. struct regval *rv;
  468. switch (dev->vsettings.mode) {
  469. case MODE_QCIF: com7 = COM7_FMT_QCIF;
  470. dummylines = 604;
  471. break;
  472. case MODE_QVGA: com7 = COM7_FMT_QVGA;
  473. dummylines = 267;
  474. break;
  475. case MODE_CIF: com7 = COM7_FMT_CIF;
  476. dummylines = 412;
  477. break;
  478. case MODE_VGA: com7 = COM7_FMT_VGA;
  479. dummylines = 11;
  480. break;
  481. case MODE_SXGA: com7 = COM7_FMT_SXGA;
  482. dummylines = 0;
  483. break;
  484. default: STK_ERROR("Unsupported mode %d\n", dev->vsettings.mode);
  485. return -EFAULT;
  486. }
  487. switch (dev->vsettings.palette) {
  488. case V4L2_PIX_FMT_UYVY:
  489. com7 |= COM7_YUV;
  490. rv = ov_fmt_uyvy;
  491. break;
  492. case V4L2_PIX_FMT_YUYV:
  493. com7 |= COM7_YUV;
  494. rv = ov_fmt_yuyv;
  495. break;
  496. case V4L2_PIX_FMT_RGB565:
  497. com7 |= COM7_RGB;
  498. rv = ov_fmt_rgbp;
  499. break;
  500. case V4L2_PIX_FMT_RGB565X:
  501. com7 |= COM7_RGB;
  502. rv = ov_fmt_rgbr;
  503. break;
  504. case V4L2_PIX_FMT_SBGGR8:
  505. com7 |= COM7_PBAYER;
  506. rv = ov_fmt_bayer;
  507. break;
  508. default: STK_ERROR("Unsupported colorspace\n");
  509. return -EFAULT;
  510. }
  511. /*FIXME sometimes the sensor go to a bad state
  512. stk_sensor_write_regvals(dev, ov_initvals); */
  513. stk_sensor_outb(dev, REG_COM7, com7);
  514. msleep(50);
  515. stk_sensor_write_regvals(dev, rv);
  516. flip = (dev->vsettings.vflip?MVFP_FLIP:0)
  517. | (dev->vsettings.hflip?MVFP_MIRROR:0);
  518. stk_sensor_outb(dev, REG_MVFP, flip);
  519. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8
  520. && !dev->vsettings.vflip)
  521. stk_sensor_outb(dev, REG_TSLB, 0x08);
  522. stk_sensor_outb(dev, REG_ADVFH, dummylines >> 8);
  523. stk_sensor_outb(dev, REG_ADVFL, dummylines & 0xff);
  524. msleep(50);
  525. switch (dev->vsettings.mode) {
  526. case MODE_VGA:
  527. if (stk_sensor_set_hw(dev, 302, 1582, 6, 486))
  528. STK_ERROR("stk_sensor_set_hw failed (VGA)\n");
  529. break;
  530. case MODE_SXGA:
  531. case MODE_CIF:
  532. case MODE_QVGA:
  533. case MODE_QCIF:
  534. /*FIXME These settings seem ignored by the sensor
  535. if (stk_sensor_set_hw(dev, 220, 1500, 10, 1034))
  536. STK_ERROR("stk_sensor_set_hw failed (SXGA)\n");
  537. */
  538. break;
  539. }
  540. msleep(10);
  541. return 0;
  542. }
  543. int stk_sensor_set_brightness(struct stk_camera *dev, int br)
  544. {
  545. if (br < 0 || br > 0xff)
  546. return -EINVAL;
  547. stk_sensor_outb(dev, REG_AEB, max(0x00, br - 6));
  548. stk_sensor_outb(dev, REG_AEW, min(0xff, br + 6));
  549. return 0;
  550. }