stk-sensor.c 19 KB

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