ov7x20.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* OmniVision OV7620/OV7120 Camera Chip Support Code
  2. *
  3. * Copyright (c) 1999-2004 Mark McClelland <mark@alpha.dyndns.org>
  4. * http://alpha.dyndns.org/ov511/
  5. *
  6. * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
  12. */
  13. #define DEBUG
  14. #include <linux/slab.h>
  15. #include "ovcamchip_priv.h"
  16. /* Registers */
  17. #define REG_GAIN 0x00 /* gain [5:0] */
  18. #define REG_BLUE 0x01 /* blue gain */
  19. #define REG_RED 0x02 /* red gain */
  20. #define REG_SAT 0x03 /* saturation */
  21. #define REG_BRT 0x06 /* Y brightness */
  22. #define REG_SHARP 0x07 /* analog sharpness */
  23. #define REG_BLUE_BIAS 0x0C /* WB blue ratio [5:0] */
  24. #define REG_RED_BIAS 0x0D /* WB red ratio [5:0] */
  25. #define REG_EXP 0x10 /* exposure */
  26. /* Default control settings. Values are in terms of V4L2 controls. */
  27. #define OV7120_DFL_BRIGHT 0x60
  28. #define OV7620_DFL_BRIGHT 0x60
  29. #define OV7120_DFL_SAT 0xb0
  30. #define OV7620_DFL_SAT 0xc0
  31. #define DFL_AUTO_EXP 1
  32. #define DFL_AUTO_GAIN 1
  33. #define OV7120_DFL_GAIN 0x00
  34. #define OV7620_DFL_GAIN 0x00
  35. /* NOTE: Since autoexposure is the default, these aren't programmed into the
  36. * OV7x20 chip. They are just here because V4L2 expects a default */
  37. #define OV7120_DFL_EXP 0x7f
  38. #define OV7620_DFL_EXP 0x7f
  39. /* Window parameters */
  40. #define HWSBASE 0x2F /* From 7620.SET (spec is wrong) */
  41. #define HWEBASE 0x2F
  42. #define VWSBASE 0x05
  43. #define VWEBASE 0x05
  44. struct ov7x20 {
  45. int auto_brt;
  46. int auto_exp;
  47. int auto_gain;
  48. int backlight;
  49. int bandfilt;
  50. int mirror;
  51. };
  52. /* Contrast look-up table */
  53. static unsigned char ctab[] = {
  54. 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
  55. 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
  56. };
  57. /* Settings for (Black & White) OV7120 camera chip */
  58. static struct ovcamchip_regvals regvals_init_7120[] = {
  59. { 0x12, 0x80 }, /* reset */
  60. { 0x13, 0x00 }, /* Autoadjust off */
  61. { 0x12, 0x20 }, /* Disable AWB */
  62. { 0x13, DFL_AUTO_GAIN?0x01:0x00 }, /* Autoadjust on (if desired) */
  63. { 0x00, OV7120_DFL_GAIN },
  64. { 0x01, 0x80 },
  65. { 0x02, 0x80 },
  66. { 0x03, OV7120_DFL_SAT },
  67. { 0x06, OV7120_DFL_BRIGHT },
  68. { 0x07, 0x00 },
  69. { 0x0c, 0x20 },
  70. { 0x0d, 0x20 },
  71. { 0x11, 0x01 },
  72. { 0x14, 0x84 },
  73. { 0x15, 0x01 },
  74. { 0x16, 0x03 },
  75. { 0x17, 0x2f },
  76. { 0x18, 0xcf },
  77. { 0x19, 0x06 },
  78. { 0x1a, 0xf5 },
  79. { 0x1b, 0x00 },
  80. { 0x20, 0x08 },
  81. { 0x21, 0x80 },
  82. { 0x22, 0x80 },
  83. { 0x23, 0x00 },
  84. { 0x26, 0xa0 },
  85. { 0x27, 0xfa },
  86. { 0x28, 0x20 }, /* DON'T set bit 6. It is for the OV7620 only */
  87. { 0x29, DFL_AUTO_EXP?0x00:0x80 },
  88. { 0x2a, 0x10 },
  89. { 0x2b, 0x00 },
  90. { 0x2c, 0x88 },
  91. { 0x2d, 0x95 },
  92. { 0x2e, 0x80 },
  93. { 0x2f, 0x44 },
  94. { 0x60, 0x20 },
  95. { 0x61, 0x02 },
  96. { 0x62, 0x5f },
  97. { 0x63, 0xd5 },
  98. { 0x64, 0x57 },
  99. { 0x65, 0x83 }, /* OV says "don't change this value" */
  100. { 0x66, 0x55 },
  101. { 0x67, 0x92 },
  102. { 0x68, 0xcf },
  103. { 0x69, 0x76 },
  104. { 0x6a, 0x22 },
  105. { 0x6b, 0xe2 },
  106. { 0x6c, 0x40 },
  107. { 0x6d, 0x48 },
  108. { 0x6e, 0x80 },
  109. { 0x6f, 0x0d },
  110. { 0x70, 0x89 },
  111. { 0x71, 0x00 },
  112. { 0x72, 0x14 },
  113. { 0x73, 0x54 },
  114. { 0x74, 0xa0 },
  115. { 0x75, 0x8e },
  116. { 0x76, 0x00 },
  117. { 0x77, 0xff },
  118. { 0x78, 0x80 },
  119. { 0x79, 0x80 },
  120. { 0x7a, 0x80 },
  121. { 0x7b, 0xe6 },
  122. { 0x7c, 0x00 },
  123. { 0x24, 0x3a },
  124. { 0x25, 0x60 },
  125. { 0xff, 0xff }, /* END MARKER */
  126. };
  127. /* Settings for (color) OV7620 camera chip */
  128. static struct ovcamchip_regvals regvals_init_7620[] = {
  129. { 0x12, 0x80 }, /* reset */
  130. { 0x00, OV7620_DFL_GAIN },
  131. { 0x01, 0x80 },
  132. { 0x02, 0x80 },
  133. { 0x03, OV7620_DFL_SAT },
  134. { 0x06, OV7620_DFL_BRIGHT },
  135. { 0x07, 0x00 },
  136. { 0x0c, 0x24 },
  137. { 0x0c, 0x24 },
  138. { 0x0d, 0x24 },
  139. { 0x11, 0x01 },
  140. { 0x12, 0x24 },
  141. { 0x13, DFL_AUTO_GAIN?0x01:0x00 },
  142. { 0x14, 0x84 },
  143. { 0x15, 0x01 },
  144. { 0x16, 0x03 },
  145. { 0x17, 0x2f },
  146. { 0x18, 0xcf },
  147. { 0x19, 0x06 },
  148. { 0x1a, 0xf5 },
  149. { 0x1b, 0x00 },
  150. { 0x20, 0x18 },
  151. { 0x21, 0x80 },
  152. { 0x22, 0x80 },
  153. { 0x23, 0x00 },
  154. { 0x26, 0xa2 },
  155. { 0x27, 0xea },
  156. { 0x28, 0x20 },
  157. { 0x29, DFL_AUTO_EXP?0x00:0x80 },
  158. { 0x2a, 0x10 },
  159. { 0x2b, 0x00 },
  160. { 0x2c, 0x88 },
  161. { 0x2d, 0x91 },
  162. { 0x2e, 0x80 },
  163. { 0x2f, 0x44 },
  164. { 0x60, 0x27 },
  165. { 0x61, 0x02 },
  166. { 0x62, 0x5f },
  167. { 0x63, 0xd5 },
  168. { 0x64, 0x57 },
  169. { 0x65, 0x83 },
  170. { 0x66, 0x55 },
  171. { 0x67, 0x92 },
  172. { 0x68, 0xcf },
  173. { 0x69, 0x76 },
  174. { 0x6a, 0x22 },
  175. { 0x6b, 0x00 },
  176. { 0x6c, 0x02 },
  177. { 0x6d, 0x44 },
  178. { 0x6e, 0x80 },
  179. { 0x6f, 0x1d },
  180. { 0x70, 0x8b },
  181. { 0x71, 0x00 },
  182. { 0x72, 0x14 },
  183. { 0x73, 0x54 },
  184. { 0x74, 0x00 },
  185. { 0x75, 0x8e },
  186. { 0x76, 0x00 },
  187. { 0x77, 0xff },
  188. { 0x78, 0x80 },
  189. { 0x79, 0x80 },
  190. { 0x7a, 0x80 },
  191. { 0x7b, 0xe2 },
  192. { 0x7c, 0x00 },
  193. { 0xff, 0xff }, /* END MARKER */
  194. };
  195. /* Returns index into the specified look-up table, with 'n' elements, for which
  196. * the value is greater than or equal to "val". If a match isn't found, (n-1)
  197. * is returned. The entries in the table must be in ascending order. */
  198. static inline int ov7x20_lut_find(unsigned char lut[], int n, unsigned char val)
  199. {
  200. int i = 0;
  201. while (lut[i] < val && i < n)
  202. i++;
  203. return i;
  204. }
  205. /* This initializes the OV7x20 camera chip and relevant variables. */
  206. static int ov7x20_init(struct i2c_client *c)
  207. {
  208. struct ovcamchip *ov = i2c_get_clientdata(c);
  209. struct ov7x20 *s;
  210. int rc;
  211. DDEBUG(4, &c->dev, "entered");
  212. if (ov->mono)
  213. rc = ov_write_regvals(c, regvals_init_7120);
  214. else
  215. rc = ov_write_regvals(c, regvals_init_7620);
  216. if (rc < 0)
  217. return rc;
  218. ov->spriv = s = kzalloc(sizeof *s, GFP_KERNEL);
  219. if (!s)
  220. return -ENOMEM;
  221. s->auto_brt = 1;
  222. s->auto_exp = DFL_AUTO_EXP;
  223. s->auto_gain = DFL_AUTO_GAIN;
  224. return 0;
  225. }
  226. static int ov7x20_free(struct i2c_client *c)
  227. {
  228. struct ovcamchip *ov = i2c_get_clientdata(c);
  229. kfree(ov->spriv);
  230. return 0;
  231. }
  232. static int ov7x20_set_v4l1_control(struct i2c_client *c,
  233. struct ovcamchip_control *ctl)
  234. {
  235. struct ovcamchip *ov = i2c_get_clientdata(c);
  236. struct ov7x20 *s = ov->spriv;
  237. int rc;
  238. int v = ctl->value;
  239. switch (ctl->id) {
  240. case OVCAMCHIP_CID_CONT:
  241. {
  242. /* Use Y gamma control instead. Bit 0 enables it. */
  243. rc = ov_write(c, 0x64, ctab[v >> 12]);
  244. break;
  245. }
  246. case OVCAMCHIP_CID_BRIGHT:
  247. /* 7620 doesn't like manual changes when in auto mode */
  248. if (!s->auto_brt)
  249. rc = ov_write(c, REG_BRT, v >> 8);
  250. else
  251. rc = 0;
  252. break;
  253. case OVCAMCHIP_CID_SAT:
  254. rc = ov_write(c, REG_SAT, v >> 8);
  255. break;
  256. case OVCAMCHIP_CID_EXP:
  257. if (!s->auto_exp)
  258. rc = ov_write(c, REG_EXP, v);
  259. else
  260. rc = -EBUSY;
  261. break;
  262. case OVCAMCHIP_CID_FREQ:
  263. {
  264. int sixty = (v == 60);
  265. rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
  266. if (rc < 0)
  267. goto out;
  268. rc = ov_write(c, 0x2b, sixty?0x00:0xac);
  269. if (rc < 0)
  270. goto out;
  271. rc = ov_write_mask(c, 0x76, 0x01, 0x01);
  272. break;
  273. }
  274. case OVCAMCHIP_CID_BANDFILT:
  275. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  276. s->bandfilt = v;
  277. break;
  278. case OVCAMCHIP_CID_AUTOBRIGHT:
  279. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  280. s->auto_brt = v;
  281. break;
  282. case OVCAMCHIP_CID_AUTOEXP:
  283. rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
  284. s->auto_exp = v;
  285. break;
  286. case OVCAMCHIP_CID_BACKLIGHT:
  287. {
  288. rc = ov_write_mask(c, 0x68, v?0xe0:0xc0, 0xe0);
  289. if (rc < 0)
  290. goto out;
  291. rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
  292. if (rc < 0)
  293. goto out;
  294. rc = ov_write_mask(c, 0x28, v?0x02:0x00, 0x02);
  295. s->backlight = v;
  296. break;
  297. }
  298. case OVCAMCHIP_CID_MIRROR:
  299. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  300. s->mirror = v;
  301. break;
  302. default:
  303. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  304. return -EPERM;
  305. }
  306. out:
  307. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  308. return rc;
  309. }
  310. static int ov7x20_get_v4l1_control(struct i2c_client *c,
  311. struct ovcamchip_control *ctl)
  312. {
  313. struct ovcamchip *ov = i2c_get_clientdata(c);
  314. struct ov7x20 *s = ov->spriv;
  315. int rc = 0;
  316. unsigned char val = 0;
  317. switch (ctl->id) {
  318. case OVCAMCHIP_CID_CONT:
  319. rc = ov_read(c, 0x64, &val);
  320. ctl->value = ov7x20_lut_find(ctab, 16, val) << 12;
  321. break;
  322. case OVCAMCHIP_CID_BRIGHT:
  323. rc = ov_read(c, REG_BRT, &val);
  324. ctl->value = val << 8;
  325. break;
  326. case OVCAMCHIP_CID_SAT:
  327. rc = ov_read(c, REG_SAT, &val);
  328. ctl->value = val << 8;
  329. break;
  330. case OVCAMCHIP_CID_EXP:
  331. rc = ov_read(c, REG_EXP, &val);
  332. ctl->value = val;
  333. break;
  334. case OVCAMCHIP_CID_BANDFILT:
  335. ctl->value = s->bandfilt;
  336. break;
  337. case OVCAMCHIP_CID_AUTOBRIGHT:
  338. ctl->value = s->auto_brt;
  339. break;
  340. case OVCAMCHIP_CID_AUTOEXP:
  341. ctl->value = s->auto_exp;
  342. break;
  343. case OVCAMCHIP_CID_BACKLIGHT:
  344. ctl->value = s->backlight;
  345. break;
  346. case OVCAMCHIP_CID_MIRROR:
  347. ctl->value = s->mirror;
  348. break;
  349. default:
  350. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  351. return -EPERM;
  352. }
  353. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  354. return rc;
  355. }
  356. static int ov7x20_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  357. {
  358. struct ovcamchip *ov = i2c_get_clientdata(c);
  359. int qvga = win->quarter;
  360. /******** QVGA-specific regs ********/
  361. ov_write_mask(c, 0x14, qvga?0x20:0x00, 0x20);
  362. ov_write_mask(c, 0x28, qvga?0x00:0x20, 0x20);
  363. ov_write(c, 0x24, qvga?0x20:0x3a);
  364. ov_write(c, 0x25, qvga?0x30:0x60);
  365. ov_write_mask(c, 0x2d, qvga?0x40:0x00, 0x40);
  366. if (!ov->mono)
  367. ov_write_mask(c, 0x67, qvga?0xf0:0x90, 0xf0);
  368. ov_write_mask(c, 0x74, qvga?0x20:0x00, 0x20);
  369. /******** Clock programming ********/
  370. ov_write(c, 0x11, win->clockdiv);
  371. return 0;
  372. }
  373. static int ov7x20_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  374. {
  375. int ret, hwscale, vwscale;
  376. ret = ov7x20_mode_init(c, win);
  377. if (ret < 0)
  378. return ret;
  379. if (win->quarter) {
  380. hwscale = 1;
  381. vwscale = 0;
  382. } else {
  383. hwscale = 2;
  384. vwscale = 1;
  385. }
  386. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  387. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  388. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  389. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  390. return 0;
  391. }
  392. static int ov7x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
  393. {
  394. switch (cmd) {
  395. case OVCAMCHIP_CMD_S_CTRL:
  396. return ov7x20_set_v4l1_control(c, arg);
  397. case OVCAMCHIP_CMD_G_CTRL:
  398. return ov7x20_get_v4l1_control(c, arg);
  399. case OVCAMCHIP_CMD_S_MODE:
  400. return ov7x20_set_window(c, arg);
  401. default:
  402. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  403. return -ENOIOCTLCMD;
  404. }
  405. }
  406. struct ovcamchip_ops ov7x20_ops = {
  407. .init = ov7x20_init,
  408. .free = ov7x20_free,
  409. .command = ov7x20_command,
  410. };