ov76be.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 = kzalloc(sizeof *s, GFP_KERNEL);
  99. if (!s)
  100. return -ENOMEM;
  101. s->auto_brt = 1;
  102. s->auto_exp = 1;
  103. return rc;
  104. }
  105. static int ov76be_free(struct i2c_client *c)
  106. {
  107. struct ovcamchip *ov = i2c_get_clientdata(c);
  108. kfree(ov->spriv);
  109. return 0;
  110. }
  111. static int ov76be_set_control(struct i2c_client *c,
  112. struct ovcamchip_control *ctl)
  113. {
  114. struct ovcamchip *ov = i2c_get_clientdata(c);
  115. struct ov76be *s = ov->spriv;
  116. int rc;
  117. int v = ctl->value;
  118. switch (ctl->id) {
  119. case OVCAMCHIP_CID_BRIGHT:
  120. rc = ov_write(c, REG_BRT, v >> 8);
  121. break;
  122. case OVCAMCHIP_CID_SAT:
  123. rc = ov_write(c, REG_SAT, v >> 8);
  124. break;
  125. case OVCAMCHIP_CID_EXP:
  126. rc = ov_write(c, REG_EXP, v);
  127. break;
  128. case OVCAMCHIP_CID_FREQ:
  129. {
  130. int sixty = (v == 60);
  131. rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
  132. if (rc < 0)
  133. goto out;
  134. rc = ov_write(c, 0x2b, sixty?0x00:0xac);
  135. if (rc < 0)
  136. goto out;
  137. rc = ov_write_mask(c, 0x76, 0x01, 0x01);
  138. break;
  139. }
  140. case OVCAMCHIP_CID_BANDFILT:
  141. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  142. s->bandfilt = v;
  143. break;
  144. case OVCAMCHIP_CID_AUTOBRIGHT:
  145. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  146. s->auto_brt = v;
  147. break;
  148. case OVCAMCHIP_CID_AUTOEXP:
  149. rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
  150. s->auto_exp = v;
  151. break;
  152. case OVCAMCHIP_CID_MIRROR:
  153. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  154. s->mirror = v;
  155. break;
  156. default:
  157. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  158. return -EPERM;
  159. }
  160. out:
  161. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  162. return rc;
  163. }
  164. static int ov76be_get_control(struct i2c_client *c,
  165. struct ovcamchip_control *ctl)
  166. {
  167. struct ovcamchip *ov = i2c_get_clientdata(c);
  168. struct ov76be *s = ov->spriv;
  169. int rc = 0;
  170. unsigned char val = 0;
  171. switch (ctl->id) {
  172. case OVCAMCHIP_CID_BRIGHT:
  173. rc = ov_read(c, REG_BRT, &val);
  174. ctl->value = val << 8;
  175. break;
  176. case OVCAMCHIP_CID_SAT:
  177. rc = ov_read(c, REG_SAT, &val);
  178. ctl->value = val << 8;
  179. break;
  180. case OVCAMCHIP_CID_EXP:
  181. rc = ov_read(c, REG_EXP, &val);
  182. ctl->value = val;
  183. break;
  184. case OVCAMCHIP_CID_BANDFILT:
  185. ctl->value = s->bandfilt;
  186. break;
  187. case OVCAMCHIP_CID_AUTOBRIGHT:
  188. ctl->value = s->auto_brt;
  189. break;
  190. case OVCAMCHIP_CID_AUTOEXP:
  191. ctl->value = s->auto_exp;
  192. break;
  193. case OVCAMCHIP_CID_MIRROR:
  194. ctl->value = s->mirror;
  195. break;
  196. default:
  197. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  198. return -EPERM;
  199. }
  200. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  201. return rc;
  202. }
  203. static int ov76be_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  204. {
  205. int qvga = win->quarter;
  206. /******** QVGA-specific regs ********/
  207. ov_write(c, 0x14, qvga?0xa4:0x84);
  208. /******** Palette-specific regs ********/
  209. if (win->format == VIDEO_PALETTE_GREY) {
  210. ov_write_mask(c, 0x0e, 0x40, 0x40);
  211. ov_write_mask(c, 0x13, 0x20, 0x20);
  212. } else {
  213. ov_write_mask(c, 0x0e, 0x00, 0x40);
  214. ov_write_mask(c, 0x13, 0x00, 0x20);
  215. }
  216. /******** Clock programming ********/
  217. ov_write(c, 0x11, win->clockdiv);
  218. /******** Resolution-specific ********/
  219. if (win->width == 640 && win->height == 480)
  220. ov_write(c, 0x35, 0x9e);
  221. else
  222. ov_write(c, 0x35, 0x1e);
  223. return 0;
  224. }
  225. static int ov76be_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  226. {
  227. int ret, hwscale, vwscale;
  228. ret = ov76be_mode_init(c, win);
  229. if (ret < 0)
  230. return ret;
  231. if (win->quarter) {
  232. hwscale = 1;
  233. vwscale = 0;
  234. } else {
  235. hwscale = 2;
  236. vwscale = 1;
  237. }
  238. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  239. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  240. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  241. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  242. return 0;
  243. }
  244. static int ov76be_command(struct i2c_client *c, unsigned int cmd, void *arg)
  245. {
  246. switch (cmd) {
  247. case OVCAMCHIP_CMD_S_CTRL:
  248. return ov76be_set_control(c, arg);
  249. case OVCAMCHIP_CMD_G_CTRL:
  250. return ov76be_get_control(c, arg);
  251. case OVCAMCHIP_CMD_S_MODE:
  252. return ov76be_set_window(c, arg);
  253. default:
  254. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  255. return -ENOIOCTLCMD;
  256. }
  257. }
  258. struct ovcamchip_ops ov76be_ops = {
  259. .init = ov76be_init,
  260. .free = ov76be_free,
  261. .command = ov76be_command,
  262. };