ov6x20.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* OmniVision OV6620/OV6120 Camera Chip Support Code
  2. *
  3. * Copyright (c) 1999-2004 Mark McClelland <mark@alpha.dyndns.org>
  4. * http://alpha.dyndns.org/ov511/
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
  10. */
  11. #define DEBUG
  12. #include <linux/slab.h>
  13. #include "ovcamchip_priv.h"
  14. /* Registers */
  15. #define REG_GAIN 0x00 /* gain [5:0] */
  16. #define REG_BLUE 0x01 /* blue gain */
  17. #define REG_RED 0x02 /* red gain */
  18. #define REG_SAT 0x03 /* saturation */
  19. #define REG_CNT 0x05 /* Y contrast */
  20. #define REG_BRT 0x06 /* Y brightness */
  21. #define REG_WB_BLUE 0x0C /* WB blue ratio [5:0] */
  22. #define REG_WB_RED 0x0D /* WB red ratio [5:0] */
  23. #define REG_EXP 0x10 /* exposure */
  24. /* Window parameters */
  25. #define HWSBASE 0x38
  26. #define HWEBASE 0x3A
  27. #define VWSBASE 0x05
  28. #define VWEBASE 0x06
  29. struct ov6x20 {
  30. int auto_brt;
  31. int auto_exp;
  32. int backlight;
  33. int bandfilt;
  34. int mirror;
  35. };
  36. /* Initial values for use with OV511/OV511+ cameras */
  37. static struct ovcamchip_regvals regvals_init_6x20_511[] = {
  38. { 0x12, 0x80 }, /* reset */
  39. { 0x11, 0x01 },
  40. { 0x03, 0x60 },
  41. { 0x05, 0x7f }, /* For when autoadjust is off */
  42. { 0x07, 0xa8 },
  43. { 0x0c, 0x24 },
  44. { 0x0d, 0x24 },
  45. { 0x0f, 0x15 }, /* COMS */
  46. { 0x10, 0x75 }, /* AEC Exposure time */
  47. { 0x12, 0x24 }, /* Enable AGC and AWB */
  48. { 0x14, 0x04 },
  49. { 0x16, 0x03 },
  50. { 0x26, 0xb2 }, /* BLC enable */
  51. /* 0x28: 0x05 Selects RGB format if RGB on */
  52. { 0x28, 0x05 },
  53. { 0x2a, 0x04 }, /* Disable framerate adjust */
  54. { 0x2d, 0x99 },
  55. { 0x33, 0xa0 }, /* Color Processing Parameter */
  56. { 0x34, 0xd2 }, /* Max A/D range */
  57. { 0x38, 0x8b },
  58. { 0x39, 0x40 },
  59. { 0x3c, 0x39 }, /* Enable AEC mode changing */
  60. { 0x3c, 0x3c }, /* Change AEC mode */
  61. { 0x3c, 0x24 }, /* Disable AEC mode changing */
  62. { 0x3d, 0x80 },
  63. /* These next two registers (0x4a, 0x4b) are undocumented. They
  64. * control the color balance */
  65. { 0x4a, 0x80 },
  66. { 0x4b, 0x80 },
  67. { 0x4d, 0xd2 }, /* This reduces noise a bit */
  68. { 0x4e, 0xc1 },
  69. { 0x4f, 0x04 },
  70. { 0xff, 0xff }, /* END MARKER */
  71. };
  72. /* Initial values for use with OV518 cameras */
  73. static struct ovcamchip_regvals regvals_init_6x20_518[] = {
  74. { 0x12, 0x80 }, /* Do a reset */
  75. { 0x03, 0xc0 }, /* Saturation */
  76. { 0x05, 0x8a }, /* Contrast */
  77. { 0x0c, 0x24 }, /* AWB blue */
  78. { 0x0d, 0x24 }, /* AWB red */
  79. { 0x0e, 0x8d }, /* Additional 2x gain */
  80. { 0x0f, 0x25 }, /* Black expanding level = 1.3V */
  81. { 0x11, 0x01 }, /* Clock div. */
  82. { 0x12, 0x24 }, /* Enable AGC and AWB */
  83. { 0x13, 0x01 }, /* (default) */
  84. { 0x14, 0x80 }, /* Set reserved bit 7 */
  85. { 0x15, 0x01 }, /* (default) */
  86. { 0x16, 0x03 }, /* (default) */
  87. { 0x17, 0x38 }, /* (default) */
  88. { 0x18, 0xea }, /* (default) */
  89. { 0x19, 0x04 },
  90. { 0x1a, 0x93 },
  91. { 0x1b, 0x00 }, /* (default) */
  92. { 0x1e, 0xc4 }, /* (default) */
  93. { 0x1f, 0x04 }, /* (default) */
  94. { 0x20, 0x20 }, /* Enable 1st stage aperture correction */
  95. { 0x21, 0x10 }, /* Y offset */
  96. { 0x22, 0x88 }, /* U offset */
  97. { 0x23, 0xc0 }, /* Set XTAL power level */
  98. { 0x24, 0x53 }, /* AEC bright ratio */
  99. { 0x25, 0x7a }, /* AEC black ratio */
  100. { 0x26, 0xb2 }, /* BLC enable */
  101. { 0x27, 0xa2 }, /* Full output range */
  102. { 0x28, 0x01 }, /* (default) */
  103. { 0x29, 0x00 }, /* (default) */
  104. { 0x2a, 0x84 }, /* (default) */
  105. { 0x2b, 0xa8 }, /* Set custom frame rate */
  106. { 0x2c, 0xa0 }, /* (reserved) */
  107. { 0x2d, 0x95 }, /* Enable banding filter */
  108. { 0x2e, 0x88 }, /* V offset */
  109. { 0x33, 0x22 }, /* Luminance gamma on */
  110. { 0x34, 0xc7 }, /* A/D bias */
  111. { 0x36, 0x12 }, /* (reserved) */
  112. { 0x37, 0x63 }, /* (reserved) */
  113. { 0x38, 0x8b }, /* Quick AEC/AEB */
  114. { 0x39, 0x00 }, /* (default) */
  115. { 0x3a, 0x0f }, /* (default) */
  116. { 0x3b, 0x3c }, /* (default) */
  117. { 0x3c, 0x5c }, /* AEC controls */
  118. { 0x3d, 0x80 }, /* Drop 1 (bad) frame when AEC change */
  119. { 0x3e, 0x80 }, /* (default) */
  120. { 0x3f, 0x02 }, /* (default) */
  121. { 0x40, 0x10 }, /* (reserved) */
  122. { 0x41, 0x10 }, /* (reserved) */
  123. { 0x42, 0x00 }, /* (reserved) */
  124. { 0x43, 0x7f }, /* (reserved) */
  125. { 0x44, 0x80 }, /* (reserved) */
  126. { 0x45, 0x1c }, /* (reserved) */
  127. { 0x46, 0x1c }, /* (reserved) */
  128. { 0x47, 0x80 }, /* (reserved) */
  129. { 0x48, 0x5f }, /* (reserved) */
  130. { 0x49, 0x00 }, /* (reserved) */
  131. { 0x4a, 0x00 }, /* Color balance (undocumented) */
  132. { 0x4b, 0x80 }, /* Color balance (undocumented) */
  133. { 0x4c, 0x58 }, /* (reserved) */
  134. { 0x4d, 0xd2 }, /* U *= .938, V *= .838 */
  135. { 0x4e, 0xa0 }, /* (default) */
  136. { 0x4f, 0x04 }, /* UV 3-point average */
  137. { 0x50, 0xff }, /* (reserved) */
  138. { 0x51, 0x58 }, /* (reserved) */
  139. { 0x52, 0xc0 }, /* (reserved) */
  140. { 0x53, 0x42 }, /* (reserved) */
  141. { 0x27, 0xa6 }, /* Enable manual offset adj. (reg 21 & 22) */
  142. { 0x12, 0x20 },
  143. { 0x12, 0x24 },
  144. { 0xff, 0xff }, /* END MARKER */
  145. };
  146. /* This initializes the OV6x20 camera chip and relevant variables. */
  147. static int ov6x20_init(struct i2c_client *c)
  148. {
  149. struct ovcamchip *ov = i2c_get_clientdata(c);
  150. struct ov6x20 *s;
  151. int rc;
  152. DDEBUG(4, &c->dev, "entered");
  153. switch (c->adapter->id) {
  154. case I2C_HW_SMBUS_OV511:
  155. rc = ov_write_regvals(c, regvals_init_6x20_511);
  156. break;
  157. case I2C_HW_SMBUS_OV518:
  158. rc = ov_write_regvals(c, regvals_init_6x20_518);
  159. break;
  160. default:
  161. dev_err(&c->dev, "ov6x20: Unsupported adapter\n");
  162. rc = -ENODEV;
  163. }
  164. if (rc < 0)
  165. return rc;
  166. ov->spriv = s = kzalloc(sizeof *s, GFP_KERNEL);
  167. if (!s)
  168. return -ENOMEM;
  169. s->auto_brt = 1;
  170. s->auto_exp = 1;
  171. return rc;
  172. }
  173. static int ov6x20_free(struct i2c_client *c)
  174. {
  175. struct ovcamchip *ov = i2c_get_clientdata(c);
  176. kfree(ov->spriv);
  177. return 0;
  178. }
  179. static int ov6x20_set_control(struct i2c_client *c,
  180. struct ovcamchip_control *ctl)
  181. {
  182. struct ovcamchip *ov = i2c_get_clientdata(c);
  183. struct ov6x20 *s = ov->spriv;
  184. int rc;
  185. int v = ctl->value;
  186. switch (ctl->id) {
  187. case OVCAMCHIP_CID_CONT:
  188. rc = ov_write(c, REG_CNT, v >> 8);
  189. break;
  190. case OVCAMCHIP_CID_BRIGHT:
  191. rc = ov_write(c, REG_BRT, v >> 8);
  192. break;
  193. case OVCAMCHIP_CID_SAT:
  194. rc = ov_write(c, REG_SAT, v >> 8);
  195. break;
  196. case OVCAMCHIP_CID_HUE:
  197. rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
  198. if (rc < 0)
  199. goto out;
  200. rc = ov_write(c, REG_BLUE, v >> 8);
  201. break;
  202. case OVCAMCHIP_CID_EXP:
  203. rc = ov_write(c, REG_EXP, v);
  204. break;
  205. case OVCAMCHIP_CID_FREQ:
  206. {
  207. int sixty = (v == 60);
  208. rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
  209. if (rc < 0)
  210. goto out;
  211. rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
  212. break;
  213. }
  214. case OVCAMCHIP_CID_BANDFILT:
  215. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  216. s->bandfilt = v;
  217. break;
  218. case OVCAMCHIP_CID_AUTOBRIGHT:
  219. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  220. s->auto_brt = v;
  221. break;
  222. case OVCAMCHIP_CID_AUTOEXP:
  223. rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
  224. s->auto_exp = v;
  225. break;
  226. case OVCAMCHIP_CID_BACKLIGHT:
  227. {
  228. rc = ov_write_mask(c, 0x4e, v?0xe0:0xc0, 0xe0);
  229. if (rc < 0)
  230. goto out;
  231. rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
  232. if (rc < 0)
  233. goto out;
  234. rc = ov_write_mask(c, 0x0e, v?0x80:0x00, 0x80);
  235. s->backlight = v;
  236. break;
  237. }
  238. case OVCAMCHIP_CID_MIRROR:
  239. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  240. s->mirror = v;
  241. break;
  242. default:
  243. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  244. return -EPERM;
  245. }
  246. out:
  247. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  248. return rc;
  249. }
  250. static int ov6x20_get_control(struct i2c_client *c,
  251. struct ovcamchip_control *ctl)
  252. {
  253. struct ovcamchip *ov = i2c_get_clientdata(c);
  254. struct ov6x20 *s = ov->spriv;
  255. int rc = 0;
  256. unsigned char val = 0;
  257. switch (ctl->id) {
  258. case OVCAMCHIP_CID_CONT:
  259. rc = ov_read(c, REG_CNT, &val);
  260. ctl->value = val << 8;
  261. break;
  262. case OVCAMCHIP_CID_BRIGHT:
  263. rc = ov_read(c, REG_BRT, &val);
  264. ctl->value = val << 8;
  265. break;
  266. case OVCAMCHIP_CID_SAT:
  267. rc = ov_read(c, REG_SAT, &val);
  268. ctl->value = val << 8;
  269. break;
  270. case OVCAMCHIP_CID_HUE:
  271. rc = ov_read(c, REG_BLUE, &val);
  272. ctl->value = val << 8;
  273. break;
  274. case OVCAMCHIP_CID_EXP:
  275. rc = ov_read(c, REG_EXP, &val);
  276. ctl->value = val;
  277. break;
  278. case OVCAMCHIP_CID_BANDFILT:
  279. ctl->value = s->bandfilt;
  280. break;
  281. case OVCAMCHIP_CID_AUTOBRIGHT:
  282. ctl->value = s->auto_brt;
  283. break;
  284. case OVCAMCHIP_CID_AUTOEXP:
  285. ctl->value = s->auto_exp;
  286. break;
  287. case OVCAMCHIP_CID_BACKLIGHT:
  288. ctl->value = s->backlight;
  289. break;
  290. case OVCAMCHIP_CID_MIRROR:
  291. ctl->value = s->mirror;
  292. break;
  293. default:
  294. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  295. return -EPERM;
  296. }
  297. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  298. return rc;
  299. }
  300. static int ov6x20_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  301. {
  302. /******** QCIF-specific regs ********/
  303. ov_write(c, 0x14, win->quarter?0x24:0x04);
  304. /******** Palette-specific regs ********/
  305. /* OV518 needs 8 bit multiplexed in color mode, and 16 bit in B&W */
  306. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  307. if (win->format == VIDEO_PALETTE_GREY)
  308. ov_write_mask(c, 0x13, 0x00, 0x20);
  309. else
  310. ov_write_mask(c, 0x13, 0x20, 0x20);
  311. } else {
  312. if (win->format == VIDEO_PALETTE_GREY)
  313. ov_write_mask(c, 0x13, 0x20, 0x20);
  314. else
  315. ov_write_mask(c, 0x13, 0x00, 0x20);
  316. }
  317. /******** Clock programming ********/
  318. /* The OV6620 needs special handling. This prevents the
  319. * severe banding that normally occurs */
  320. /* Clock down */
  321. ov_write(c, 0x2a, 0x04);
  322. ov_write(c, 0x11, win->clockdiv);
  323. ov_write(c, 0x2a, 0x84);
  324. /* This next setting is critical. It seems to improve
  325. * the gain or the contrast. The "reserved" bits seem
  326. * to have some effect in this case. */
  327. ov_write(c, 0x2d, 0x85); /* FIXME: This messes up banding filter */
  328. return 0;
  329. }
  330. static int ov6x20_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  331. {
  332. int ret, hwscale, vwscale;
  333. ret = ov6x20_mode_init(c, win);
  334. if (ret < 0)
  335. return ret;
  336. if (win->quarter) {
  337. hwscale = 0;
  338. vwscale = 0;
  339. } else {
  340. hwscale = 1;
  341. vwscale = 1; /* The datasheet says 0; it's wrong */
  342. }
  343. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  344. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  345. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  346. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  347. return 0;
  348. }
  349. static int ov6x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
  350. {
  351. switch (cmd) {
  352. case OVCAMCHIP_CMD_S_CTRL:
  353. return ov6x20_set_control(c, arg);
  354. case OVCAMCHIP_CMD_G_CTRL:
  355. return ov6x20_get_control(c, arg);
  356. case OVCAMCHIP_CMD_S_MODE:
  357. return ov6x20_set_window(c, arg);
  358. default:
  359. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  360. return -ENOIOCTLCMD;
  361. }
  362. }
  363. struct ovcamchip_ops ov6x20_ops = {
  364. .init = ov6x20_init,
  365. .free = ov6x20_free,
  366. .command = ov6x20_command,
  367. };