ov76be.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* OmniVision OV76BE 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. /* OV7610 registers: Since the OV76BE is undocumented, we'll settle for these
  15. * for now. */
  16. #define REG_GAIN 0x00 /* gain [5:0] */
  17. #define REG_BLUE 0x01 /* blue channel balance */
  18. #define REG_RED 0x02 /* red channel balance */
  19. #define REG_SAT 0x03 /* saturation */
  20. #define REG_CNT 0x05 /* Y contrast */
  21. #define REG_BRT 0x06 /* Y brightness */
  22. #define REG_BLUE_BIAS 0x0C /* blue channel bias [5:0] */
  23. #define REG_RED_BIAS 0x0D /* red channel bias [5:0] */
  24. #define REG_GAMMA_COEFF 0x0E /* gamma settings */
  25. #define REG_WB_RANGE 0x0F /* AEC/ALC/S-AWB settings */
  26. #define REG_EXP 0x10 /* manual exposure setting */
  27. #define REG_CLOCK 0x11 /* polarity/clock prescaler */
  28. #define REG_FIELD_DIVIDE 0x16 /* field interval/mode settings */
  29. #define REG_HWIN_START 0x17 /* horizontal window start */
  30. #define REG_HWIN_END 0x18 /* horizontal window end */
  31. #define REG_VWIN_START 0x19 /* vertical window start */
  32. #define REG_VWIN_END 0x1A /* vertical window end */
  33. #define REG_PIXEL_SHIFT 0x1B /* pixel shift */
  34. #define REG_YOFFSET 0x21 /* Y channel offset */
  35. #define REG_UOFFSET 0x22 /* U channel offset */
  36. #define REG_ECW 0x24 /* exposure white level for AEC */
  37. #define REG_ECB 0x25 /* exposure black level for AEC */
  38. #define REG_FRAMERATE_H 0x2A /* frame rate MSB + misc */
  39. #define REG_FRAMERATE_L 0x2B /* frame rate LSB */
  40. #define REG_ALC 0x2C /* Auto Level Control settings */
  41. #define REG_VOFFSET 0x2E /* V channel offset adjustment */
  42. #define REG_ARRAY_BIAS 0x2F /* array bias -- don't change */
  43. #define REG_YGAMMA 0x33 /* misc gamma settings [7:6] */
  44. #define REG_BIAS_ADJUST 0x34 /* misc bias settings */
  45. /* Window parameters */
  46. #define HWSBASE 0x38
  47. #define HWEBASE 0x3a
  48. #define VWSBASE 0x05
  49. #define VWEBASE 0x05
  50. struct ov76be {
  51. int auto_brt;
  52. int auto_exp;
  53. int bandfilt;
  54. int mirror;
  55. };
  56. /* NOTE: These are the same as the 7x10 settings, but should eventually be
  57. * optimized for the OV76BE */
  58. static struct ovcamchip_regvals regvals_init_76be[] = {
  59. { 0x10, 0xff },
  60. { 0x16, 0x03 },
  61. { 0x28, 0x24 },
  62. { 0x2b, 0xac },
  63. { 0x12, 0x00 },
  64. { 0x38, 0x81 },
  65. { 0x28, 0x24 }, /* 0c */
  66. { 0x0f, 0x85 }, /* lg's setting */
  67. { 0x15, 0x01 },
  68. { 0x20, 0x1c },
  69. { 0x23, 0x2a },
  70. { 0x24, 0x10 },
  71. { 0x25, 0x8a },
  72. { 0x26, 0xa2 },
  73. { 0x27, 0xc2 },
  74. { 0x2a, 0x04 },
  75. { 0x2c, 0xfe },
  76. { 0x2d, 0x93 },
  77. { 0x30, 0x71 },
  78. { 0x31, 0x60 },
  79. { 0x32, 0x26 },
  80. { 0x33, 0x20 },
  81. { 0x34, 0x48 },
  82. { 0x12, 0x24 },
  83. { 0x11, 0x01 },
  84. { 0x0c, 0x24 },
  85. { 0x0d, 0x24 },
  86. { 0xff, 0xff }, /* END MARKER */
  87. };
  88. /* This initializes the OV76be camera chip and relevant variables. */
  89. static int ov76be_init(struct i2c_client *c)
  90. {
  91. struct ovcamchip *ov = i2c_get_clientdata(c);
  92. struct ov76be *s;
  93. int rc;
  94. DDEBUG(4, &c->dev, "entered");
  95. rc = ov_write_regvals(c, regvals_init_76be);
  96. if (rc < 0)
  97. return rc;
  98. ov->spriv = s = kmalloc(sizeof *s, GFP_KERNEL);
  99. if (!s)
  100. return -ENOMEM;
  101. memset(s, 0, sizeof *s);
  102. s->auto_brt = 1;
  103. s->auto_exp = 1;
  104. return rc;
  105. }
  106. static int ov76be_free(struct i2c_client *c)
  107. {
  108. struct ovcamchip *ov = i2c_get_clientdata(c);
  109. kfree(ov->spriv);
  110. return 0;
  111. }
  112. static int ov76be_set_control(struct i2c_client *c,
  113. struct ovcamchip_control *ctl)
  114. {
  115. struct ovcamchip *ov = i2c_get_clientdata(c);
  116. struct ov76be *s = ov->spriv;
  117. int rc;
  118. int v = ctl->value;
  119. switch (ctl->id) {
  120. case OVCAMCHIP_CID_BRIGHT:
  121. rc = ov_write(c, REG_BRT, v >> 8);
  122. break;
  123. case OVCAMCHIP_CID_SAT:
  124. rc = ov_write(c, REG_SAT, v >> 8);
  125. break;
  126. case OVCAMCHIP_CID_EXP:
  127. rc = ov_write(c, REG_EXP, v);
  128. break;
  129. case OVCAMCHIP_CID_FREQ:
  130. {
  131. int sixty = (v == 60);
  132. rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
  133. if (rc < 0)
  134. goto out;
  135. rc = ov_write(c, 0x2b, sixty?0x00:0xac);
  136. if (rc < 0)
  137. goto out;
  138. rc = ov_write_mask(c, 0x76, 0x01, 0x01);
  139. break;
  140. }
  141. case OVCAMCHIP_CID_BANDFILT:
  142. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  143. s->bandfilt = v;
  144. break;
  145. case OVCAMCHIP_CID_AUTOBRIGHT:
  146. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  147. s->auto_brt = v;
  148. break;
  149. case OVCAMCHIP_CID_AUTOEXP:
  150. rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
  151. s->auto_exp = v;
  152. break;
  153. case OVCAMCHIP_CID_MIRROR:
  154. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  155. s->mirror = v;
  156. break;
  157. default:
  158. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  159. return -EPERM;
  160. }
  161. out:
  162. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  163. return rc;
  164. }
  165. static int ov76be_get_control(struct i2c_client *c,
  166. struct ovcamchip_control *ctl)
  167. {
  168. struct ovcamchip *ov = i2c_get_clientdata(c);
  169. struct ov76be *s = ov->spriv;
  170. int rc = 0;
  171. unsigned char val = 0;
  172. switch (ctl->id) {
  173. case OVCAMCHIP_CID_BRIGHT:
  174. rc = ov_read(c, REG_BRT, &val);
  175. ctl->value = val << 8;
  176. break;
  177. case OVCAMCHIP_CID_SAT:
  178. rc = ov_read(c, REG_SAT, &val);
  179. ctl->value = val << 8;
  180. break;
  181. case OVCAMCHIP_CID_EXP:
  182. rc = ov_read(c, REG_EXP, &val);
  183. ctl->value = val;
  184. break;
  185. case OVCAMCHIP_CID_BANDFILT:
  186. ctl->value = s->bandfilt;
  187. break;
  188. case OVCAMCHIP_CID_AUTOBRIGHT:
  189. ctl->value = s->auto_brt;
  190. break;
  191. case OVCAMCHIP_CID_AUTOEXP:
  192. ctl->value = s->auto_exp;
  193. break;
  194. case OVCAMCHIP_CID_MIRROR:
  195. ctl->value = s->mirror;
  196. break;
  197. default:
  198. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  199. return -EPERM;
  200. }
  201. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  202. return rc;
  203. }
  204. static int ov76be_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  205. {
  206. int qvga = win->quarter;
  207. /******** QVGA-specific regs ********/
  208. ov_write(c, 0x14, qvga?0xa4:0x84);
  209. /******** Palette-specific regs ********/
  210. if (win->format == VIDEO_PALETTE_GREY) {
  211. ov_write_mask(c, 0x0e, 0x40, 0x40);
  212. ov_write_mask(c, 0x13, 0x20, 0x20);
  213. } else {
  214. ov_write_mask(c, 0x0e, 0x00, 0x40);
  215. ov_write_mask(c, 0x13, 0x00, 0x20);
  216. }
  217. /******** Clock programming ********/
  218. ov_write(c, 0x11, win->clockdiv);
  219. /******** Resolution-specific ********/
  220. if (win->width == 640 && win->height == 480)
  221. ov_write(c, 0x35, 0x9e);
  222. else
  223. ov_write(c, 0x35, 0x1e);
  224. return 0;
  225. }
  226. static int ov76be_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  227. {
  228. int ret, hwscale, vwscale;
  229. ret = ov76be_mode_init(c, win);
  230. if (ret < 0)
  231. return ret;
  232. if (win->quarter) {
  233. hwscale = 1;
  234. vwscale = 0;
  235. } else {
  236. hwscale = 2;
  237. vwscale = 1;
  238. }
  239. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  240. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  241. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  242. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  243. return 0;
  244. }
  245. static int ov76be_command(struct i2c_client *c, unsigned int cmd, void *arg)
  246. {
  247. switch (cmd) {
  248. case OVCAMCHIP_CMD_S_CTRL:
  249. return ov76be_set_control(c, arg);
  250. case OVCAMCHIP_CMD_G_CTRL:
  251. return ov76be_get_control(c, arg);
  252. case OVCAMCHIP_CMD_S_MODE:
  253. return ov76be_set_window(c, arg);
  254. default:
  255. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  256. return -ENOIOCTLCMD;
  257. }
  258. }
  259. struct ovcamchip_ops ov76be_ops = {
  260. .init = ov76be_init,
  261. .free = ov76be_free,
  262. .command = ov76be_command,
  263. };