ov6x20.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 = kmalloc(sizeof *s, GFP_KERNEL);
  167. if (!s)
  168. return -ENOMEM;
  169. memset(s, 0, sizeof *s);
  170. s->auto_brt = 1;
  171. s->auto_exp = 1;
  172. return rc;
  173. }
  174. static int ov6x20_free(struct i2c_client *c)
  175. {
  176. struct ovcamchip *ov = i2c_get_clientdata(c);
  177. kfree(ov->spriv);
  178. return 0;
  179. }
  180. static int ov6x20_set_control(struct i2c_client *c,
  181. struct ovcamchip_control *ctl)
  182. {
  183. struct ovcamchip *ov = i2c_get_clientdata(c);
  184. struct ov6x20 *s = ov->spriv;
  185. int rc;
  186. int v = ctl->value;
  187. switch (ctl->id) {
  188. case OVCAMCHIP_CID_CONT:
  189. rc = ov_write(c, REG_CNT, v >> 8);
  190. break;
  191. case OVCAMCHIP_CID_BRIGHT:
  192. rc = ov_write(c, REG_BRT, v >> 8);
  193. break;
  194. case OVCAMCHIP_CID_SAT:
  195. rc = ov_write(c, REG_SAT, v >> 8);
  196. break;
  197. case OVCAMCHIP_CID_HUE:
  198. rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
  199. if (rc < 0)
  200. goto out;
  201. rc = ov_write(c, REG_BLUE, v >> 8);
  202. break;
  203. case OVCAMCHIP_CID_EXP:
  204. rc = ov_write(c, REG_EXP, v);
  205. break;
  206. case OVCAMCHIP_CID_FREQ:
  207. {
  208. int sixty = (v == 60);
  209. rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
  210. if (rc < 0)
  211. goto out;
  212. rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
  213. break;
  214. }
  215. case OVCAMCHIP_CID_BANDFILT:
  216. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  217. s->bandfilt = v;
  218. break;
  219. case OVCAMCHIP_CID_AUTOBRIGHT:
  220. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  221. s->auto_brt = v;
  222. break;
  223. case OVCAMCHIP_CID_AUTOEXP:
  224. rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
  225. s->auto_exp = v;
  226. break;
  227. case OVCAMCHIP_CID_BACKLIGHT:
  228. {
  229. rc = ov_write_mask(c, 0x4e, v?0xe0:0xc0, 0xe0);
  230. if (rc < 0)
  231. goto out;
  232. rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
  233. if (rc < 0)
  234. goto out;
  235. rc = ov_write_mask(c, 0x0e, v?0x80:0x00, 0x80);
  236. s->backlight = v;
  237. break;
  238. }
  239. case OVCAMCHIP_CID_MIRROR:
  240. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  241. s->mirror = v;
  242. break;
  243. default:
  244. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  245. return -EPERM;
  246. }
  247. out:
  248. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  249. return rc;
  250. }
  251. static int ov6x20_get_control(struct i2c_client *c,
  252. struct ovcamchip_control *ctl)
  253. {
  254. struct ovcamchip *ov = i2c_get_clientdata(c);
  255. struct ov6x20 *s = ov->spriv;
  256. int rc = 0;
  257. unsigned char val = 0;
  258. switch (ctl->id) {
  259. case OVCAMCHIP_CID_CONT:
  260. rc = ov_read(c, REG_CNT, &val);
  261. ctl->value = val << 8;
  262. break;
  263. case OVCAMCHIP_CID_BRIGHT:
  264. rc = ov_read(c, REG_BRT, &val);
  265. ctl->value = val << 8;
  266. break;
  267. case OVCAMCHIP_CID_SAT:
  268. rc = ov_read(c, REG_SAT, &val);
  269. ctl->value = val << 8;
  270. break;
  271. case OVCAMCHIP_CID_HUE:
  272. rc = ov_read(c, REG_BLUE, &val);
  273. ctl->value = val << 8;
  274. break;
  275. case OVCAMCHIP_CID_EXP:
  276. rc = ov_read(c, REG_EXP, &val);
  277. ctl->value = val;
  278. break;
  279. case OVCAMCHIP_CID_BANDFILT:
  280. ctl->value = s->bandfilt;
  281. break;
  282. case OVCAMCHIP_CID_AUTOBRIGHT:
  283. ctl->value = s->auto_brt;
  284. break;
  285. case OVCAMCHIP_CID_AUTOEXP:
  286. ctl->value = s->auto_exp;
  287. break;
  288. case OVCAMCHIP_CID_BACKLIGHT:
  289. ctl->value = s->backlight;
  290. break;
  291. case OVCAMCHIP_CID_MIRROR:
  292. ctl->value = s->mirror;
  293. break;
  294. default:
  295. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  296. return -EPERM;
  297. }
  298. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  299. return rc;
  300. }
  301. static int ov6x20_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  302. {
  303. /******** QCIF-specific regs ********/
  304. ov_write(c, 0x14, win->quarter?0x24:0x04);
  305. /******** Palette-specific regs ********/
  306. /* OV518 needs 8 bit multiplexed in color mode, and 16 bit in B&W */
  307. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  308. if (win->format == VIDEO_PALETTE_GREY)
  309. ov_write_mask(c, 0x13, 0x00, 0x20);
  310. else
  311. ov_write_mask(c, 0x13, 0x20, 0x20);
  312. } else {
  313. if (win->format == VIDEO_PALETTE_GREY)
  314. ov_write_mask(c, 0x13, 0x20, 0x20);
  315. else
  316. ov_write_mask(c, 0x13, 0x00, 0x20);
  317. }
  318. /******** Clock programming ********/
  319. /* The OV6620 needs special handling. This prevents the
  320. * severe banding that normally occurs */
  321. /* Clock down */
  322. ov_write(c, 0x2a, 0x04);
  323. ov_write(c, 0x11, win->clockdiv);
  324. ov_write(c, 0x2a, 0x84);
  325. /* This next setting is critical. It seems to improve
  326. * the gain or the contrast. The "reserved" bits seem
  327. * to have some effect in this case. */
  328. ov_write(c, 0x2d, 0x85); /* FIXME: This messes up banding filter */
  329. return 0;
  330. }
  331. static int ov6x20_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  332. {
  333. int ret, hwscale, vwscale;
  334. ret = ov6x20_mode_init(c, win);
  335. if (ret < 0)
  336. return ret;
  337. if (win->quarter) {
  338. hwscale = 0;
  339. vwscale = 0;
  340. } else {
  341. hwscale = 1;
  342. vwscale = 1; /* The datasheet says 0; it's wrong */
  343. }
  344. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  345. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  346. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  347. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  348. return 0;
  349. }
  350. static int ov6x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
  351. {
  352. switch (cmd) {
  353. case OVCAMCHIP_CMD_S_CTRL:
  354. return ov6x20_set_control(c, arg);
  355. case OVCAMCHIP_CMD_G_CTRL:
  356. return ov6x20_get_control(c, arg);
  357. case OVCAMCHIP_CMD_S_MODE:
  358. return ov6x20_set_window(c, arg);
  359. default:
  360. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  361. return -ENOIOCTLCMD;
  362. }
  363. }
  364. struct ovcamchip_ops ov6x20_ops = {
  365. .init = ov6x20_init,
  366. .free = ov6x20_free,
  367. .command = ov6x20_command,
  368. };