ov7x10.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* OmniVision OV7610/OV7110 Camera Chip Support Code
  2. *
  3. * Copyright (c) 1999-2004 Mark McClelland <mark@alpha.dyndns.org>
  4. * http://alpha.dyndns.org/ov511/
  5. *
  6. * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
  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 channel balance */
  19. #define REG_RED 0x02 /* red channel balance */
  20. #define REG_SAT 0x03 /* saturation */
  21. #define REG_CNT 0x05 /* Y contrast */
  22. #define REG_BRT 0x06 /* Y brightness */
  23. #define REG_BLUE_BIAS 0x0C /* blue channel bias [5:0] */
  24. #define REG_RED_BIAS 0x0D /* red channel bias [5:0] */
  25. #define REG_GAMMA_COEFF 0x0E /* gamma settings */
  26. #define REG_WB_RANGE 0x0F /* AEC/ALC/S-AWB settings */
  27. #define REG_EXP 0x10 /* manual exposure setting */
  28. #define REG_CLOCK 0x11 /* polarity/clock prescaler */
  29. #define REG_FIELD_DIVIDE 0x16 /* field interval/mode settings */
  30. #define REG_HWIN_START 0x17 /* horizontal window start */
  31. #define REG_HWIN_END 0x18 /* horizontal window end */
  32. #define REG_VWIN_START 0x19 /* vertical window start */
  33. #define REG_VWIN_END 0x1A /* vertical window end */
  34. #define REG_PIXEL_SHIFT 0x1B /* pixel shift */
  35. #define REG_YOFFSET 0x21 /* Y channel offset */
  36. #define REG_UOFFSET 0x22 /* U channel offset */
  37. #define REG_ECW 0x24 /* exposure white level for AEC */
  38. #define REG_ECB 0x25 /* exposure black level for AEC */
  39. #define REG_FRAMERATE_H 0x2A /* frame rate MSB + misc */
  40. #define REG_FRAMERATE_L 0x2B /* frame rate LSB */
  41. #define REG_ALC 0x2C /* Auto Level Control settings */
  42. #define REG_VOFFSET 0x2E /* V channel offset adjustment */
  43. #define REG_ARRAY_BIAS 0x2F /* array bias -- don't change */
  44. #define REG_YGAMMA 0x33 /* misc gamma settings [7:6] */
  45. #define REG_BIAS_ADJUST 0x34 /* misc bias settings */
  46. /* Window parameters */
  47. #define HWSBASE 0x38
  48. #define HWEBASE 0x3a
  49. #define VWSBASE 0x05
  50. #define VWEBASE 0x05
  51. struct ov7x10 {
  52. int auto_brt;
  53. int auto_exp;
  54. int bandfilt;
  55. int mirror;
  56. };
  57. /* Lawrence Glaister <lg@jfm.bc.ca> reports:
  58. *
  59. * Register 0x0f in the 7610 has the following effects:
  60. *
  61. * 0x85 (AEC method 1): Best overall, good contrast range
  62. * 0x45 (AEC method 2): Very overexposed
  63. * 0xa5 (spec sheet default): Ok, but the black level is
  64. * shifted resulting in loss of contrast
  65. * 0x05 (old driver setting): very overexposed, too much
  66. * contrast
  67. */
  68. static struct ovcamchip_regvals regvals_init_7x10[] = {
  69. { 0x10, 0xff },
  70. { 0x16, 0x03 },
  71. { 0x28, 0x24 },
  72. { 0x2b, 0xac },
  73. { 0x12, 0x00 },
  74. { 0x38, 0x81 },
  75. { 0x28, 0x24 }, /* 0c */
  76. { 0x0f, 0x85 }, /* lg's setting */
  77. { 0x15, 0x01 },
  78. { 0x20, 0x1c },
  79. { 0x23, 0x2a },
  80. { 0x24, 0x10 },
  81. { 0x25, 0x8a },
  82. { 0x26, 0xa2 },
  83. { 0x27, 0xc2 },
  84. { 0x2a, 0x04 },
  85. { 0x2c, 0xfe },
  86. { 0x2d, 0x93 },
  87. { 0x30, 0x71 },
  88. { 0x31, 0x60 },
  89. { 0x32, 0x26 },
  90. { 0x33, 0x20 },
  91. { 0x34, 0x48 },
  92. { 0x12, 0x24 },
  93. { 0x11, 0x01 },
  94. { 0x0c, 0x24 },
  95. { 0x0d, 0x24 },
  96. { 0xff, 0xff }, /* END MARKER */
  97. };
  98. /* This initializes the OV7x10 camera chip and relevant variables. */
  99. static int ov7x10_init(struct i2c_client *c)
  100. {
  101. struct ovcamchip *ov = i2c_get_clientdata(c);
  102. struct ov7x10 *s;
  103. int rc;
  104. DDEBUG(4, &c->dev, "entered");
  105. rc = ov_write_regvals(c, regvals_init_7x10);
  106. if (rc < 0)
  107. return rc;
  108. ov->spriv = s = kmalloc(sizeof *s, GFP_KERNEL);
  109. if (!s)
  110. return -ENOMEM;
  111. memset(s, 0, sizeof *s);
  112. s->auto_brt = 1;
  113. s->auto_exp = 1;
  114. return rc;
  115. }
  116. static int ov7x10_free(struct i2c_client *c)
  117. {
  118. struct ovcamchip *ov = i2c_get_clientdata(c);
  119. kfree(ov->spriv);
  120. return 0;
  121. }
  122. static int ov7x10_set_control(struct i2c_client *c,
  123. struct ovcamchip_control *ctl)
  124. {
  125. struct ovcamchip *ov = i2c_get_clientdata(c);
  126. struct ov7x10 *s = ov->spriv;
  127. int rc;
  128. int v = ctl->value;
  129. switch (ctl->id) {
  130. case OVCAMCHIP_CID_CONT:
  131. rc = ov_write(c, REG_CNT, v >> 8);
  132. break;
  133. case OVCAMCHIP_CID_BRIGHT:
  134. rc = ov_write(c, REG_BRT, v >> 8);
  135. break;
  136. case OVCAMCHIP_CID_SAT:
  137. rc = ov_write(c, REG_SAT, v >> 8);
  138. break;
  139. case OVCAMCHIP_CID_HUE:
  140. rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
  141. if (rc < 0)
  142. goto out;
  143. rc = ov_write(c, REG_BLUE, v >> 8);
  144. break;
  145. case OVCAMCHIP_CID_EXP:
  146. rc = ov_write(c, REG_EXP, v);
  147. break;
  148. case OVCAMCHIP_CID_FREQ:
  149. {
  150. int sixty = (v == 60);
  151. rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
  152. if (rc < 0)
  153. goto out;
  154. rc = ov_write(c, 0x2b, sixty?0x00:0xac);
  155. if (rc < 0)
  156. goto out;
  157. rc = ov_write_mask(c, 0x13, 0x10, 0x10);
  158. if (rc < 0)
  159. goto out;
  160. rc = ov_write_mask(c, 0x13, 0x00, 0x10);
  161. break;
  162. }
  163. case OVCAMCHIP_CID_BANDFILT:
  164. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  165. s->bandfilt = v;
  166. break;
  167. case OVCAMCHIP_CID_AUTOBRIGHT:
  168. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  169. s->auto_brt = v;
  170. break;
  171. case OVCAMCHIP_CID_AUTOEXP:
  172. rc = ov_write_mask(c, 0x29, v?0x00:0x80, 0x80);
  173. s->auto_exp = v;
  174. break;
  175. case OVCAMCHIP_CID_MIRROR:
  176. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  177. s->mirror = v;
  178. break;
  179. default:
  180. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  181. return -EPERM;
  182. }
  183. out:
  184. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  185. return rc;
  186. }
  187. static int ov7x10_get_control(struct i2c_client *c,
  188. struct ovcamchip_control *ctl)
  189. {
  190. struct ovcamchip *ov = i2c_get_clientdata(c);
  191. struct ov7x10 *s = ov->spriv;
  192. int rc = 0;
  193. unsigned char val = 0;
  194. switch (ctl->id) {
  195. case OVCAMCHIP_CID_CONT:
  196. rc = ov_read(c, REG_CNT, &val);
  197. ctl->value = val << 8;
  198. break;
  199. case OVCAMCHIP_CID_BRIGHT:
  200. rc = ov_read(c, REG_BRT, &val);
  201. ctl->value = val << 8;
  202. break;
  203. case OVCAMCHIP_CID_SAT:
  204. rc = ov_read(c, REG_SAT, &val);
  205. ctl->value = val << 8;
  206. break;
  207. case OVCAMCHIP_CID_HUE:
  208. rc = ov_read(c, REG_BLUE, &val);
  209. ctl->value = val << 8;
  210. break;
  211. case OVCAMCHIP_CID_EXP:
  212. rc = ov_read(c, REG_EXP, &val);
  213. ctl->value = val;
  214. break;
  215. case OVCAMCHIP_CID_BANDFILT:
  216. ctl->value = s->bandfilt;
  217. break;
  218. case OVCAMCHIP_CID_AUTOBRIGHT:
  219. ctl->value = s->auto_brt;
  220. break;
  221. case OVCAMCHIP_CID_AUTOEXP:
  222. ctl->value = s->auto_exp;
  223. break;
  224. case OVCAMCHIP_CID_MIRROR:
  225. ctl->value = s->mirror;
  226. break;
  227. default:
  228. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  229. return -EPERM;
  230. }
  231. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  232. return rc;
  233. }
  234. static int ov7x10_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  235. {
  236. int qvga = win->quarter;
  237. /******** QVGA-specific regs ********/
  238. ov_write(c, 0x14, qvga?0x24:0x04);
  239. /******** Palette-specific regs ********/
  240. if (win->format == VIDEO_PALETTE_GREY) {
  241. ov_write_mask(c, 0x0e, 0x40, 0x40);
  242. ov_write_mask(c, 0x13, 0x20, 0x20);
  243. } else {
  244. ov_write_mask(c, 0x0e, 0x00, 0x40);
  245. ov_write_mask(c, 0x13, 0x00, 0x20);
  246. }
  247. /******** Clock programming ********/
  248. ov_write(c, 0x11, win->clockdiv);
  249. /******** Resolution-specific ********/
  250. if (win->width == 640 && win->height == 480)
  251. ov_write(c, 0x35, 0x9e);
  252. else
  253. ov_write(c, 0x35, 0x1e);
  254. return 0;
  255. }
  256. static int ov7x10_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  257. {
  258. int ret, hwscale, vwscale;
  259. ret = ov7x10_mode_init(c, win);
  260. if (ret < 0)
  261. return ret;
  262. if (win->quarter) {
  263. hwscale = 1;
  264. vwscale = 0;
  265. } else {
  266. hwscale = 2;
  267. vwscale = 1;
  268. }
  269. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  270. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  271. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  272. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  273. return 0;
  274. }
  275. static int ov7x10_command(struct i2c_client *c, unsigned int cmd, void *arg)
  276. {
  277. switch (cmd) {
  278. case OVCAMCHIP_CMD_S_CTRL:
  279. return ov7x10_set_control(c, arg);
  280. case OVCAMCHIP_CMD_G_CTRL:
  281. return ov7x10_get_control(c, arg);
  282. case OVCAMCHIP_CMD_S_MODE:
  283. return ov7x10_set_window(c, arg);
  284. default:
  285. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  286. return -ENOIOCTLCMD;
  287. }
  288. }
  289. struct ovcamchip_ops ov7x10_ops = {
  290. .init = ov7x10_init,
  291. .free = ov7x10_free,
  292. .command = ov7x10_command,
  293. };