stk-sensor.c 19 KB

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