ov6x30.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* OmniVision OV6630/OV6130 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 [7:3] */
  19. #define REG_CNT 0x05 /* Y contrast [3:0] */
  20. #define REG_BRT 0x06 /* Y brightness */
  21. #define REG_SHARP 0x07 /* sharpness */
  22. #define REG_WB_BLUE 0x0C /* WB blue ratio [5:0] */
  23. #define REG_WB_RED 0x0D /* WB red ratio [5:0] */
  24. #define REG_EXP 0x10 /* exposure */
  25. /* Window parameters */
  26. #define HWSBASE 0x38
  27. #define HWEBASE 0x3A
  28. #define VWSBASE 0x05
  29. #define VWEBASE 0x06
  30. struct ov6x30 {
  31. int auto_brt;
  32. int auto_exp;
  33. int backlight;
  34. int bandfilt;
  35. int mirror;
  36. };
  37. static struct ovcamchip_regvals regvals_init_6x30[] = {
  38. { 0x12, 0x80 }, /* reset */
  39. { 0x00, 0x1f }, /* Gain */
  40. { 0x01, 0x99 }, /* Blue gain */
  41. { 0x02, 0x7c }, /* Red gain */
  42. { 0x03, 0xc0 }, /* Saturation */
  43. { 0x05, 0x0a }, /* Contrast */
  44. { 0x06, 0x95 }, /* Brightness */
  45. { 0x07, 0x2d }, /* Sharpness */
  46. { 0x0c, 0x20 },
  47. { 0x0d, 0x20 },
  48. { 0x0e, 0x20 },
  49. { 0x0f, 0x05 },
  50. { 0x10, 0x9a }, /* "exposure check" */
  51. { 0x11, 0x00 }, /* Pixel clock = fastest */
  52. { 0x12, 0x24 }, /* Enable AGC and AWB */
  53. { 0x13, 0x21 },
  54. { 0x14, 0x80 },
  55. { 0x15, 0x01 },
  56. { 0x16, 0x03 },
  57. { 0x17, 0x38 },
  58. { 0x18, 0xea },
  59. { 0x19, 0x04 },
  60. { 0x1a, 0x93 },
  61. { 0x1b, 0x00 },
  62. { 0x1e, 0xc4 },
  63. { 0x1f, 0x04 },
  64. { 0x20, 0x20 },
  65. { 0x21, 0x10 },
  66. { 0x22, 0x88 },
  67. { 0x23, 0xc0 }, /* Crystal circuit power level */
  68. { 0x25, 0x9a }, /* Increase AEC black pixel ratio */
  69. { 0x26, 0xb2 }, /* BLC enable */
  70. { 0x27, 0xa2 },
  71. { 0x28, 0x00 },
  72. { 0x29, 0x00 },
  73. { 0x2a, 0x84 }, /* (keep) */
  74. { 0x2b, 0xa8 }, /* (keep) */
  75. { 0x2c, 0xa0 },
  76. { 0x2d, 0x95 }, /* Enable auto-brightness */
  77. { 0x2e, 0x88 },
  78. { 0x33, 0x26 },
  79. { 0x34, 0x03 },
  80. { 0x36, 0x8f },
  81. { 0x37, 0x80 },
  82. { 0x38, 0x83 },
  83. { 0x39, 0x80 },
  84. { 0x3a, 0x0f },
  85. { 0x3b, 0x3c },
  86. { 0x3c, 0x1a },
  87. { 0x3d, 0x80 },
  88. { 0x3e, 0x80 },
  89. { 0x3f, 0x0e },
  90. { 0x40, 0x00 }, /* White bal */
  91. { 0x41, 0x00 }, /* White bal */
  92. { 0x42, 0x80 },
  93. { 0x43, 0x3f }, /* White bal */
  94. { 0x44, 0x80 },
  95. { 0x45, 0x20 },
  96. { 0x46, 0x20 },
  97. { 0x47, 0x80 },
  98. { 0x48, 0x7f },
  99. { 0x49, 0x00 },
  100. { 0x4a, 0x00 },
  101. { 0x4b, 0x80 },
  102. { 0x4c, 0xd0 },
  103. { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
  104. { 0x4e, 0x40 },
  105. { 0x4f, 0x07 }, /* UV average mode, color killer: strongest */
  106. { 0x50, 0xff },
  107. { 0x54, 0x23 }, /* Max AGC gain: 18dB */
  108. { 0x55, 0xff },
  109. { 0x56, 0x12 },
  110. { 0x57, 0x81 }, /* (default) */
  111. { 0x58, 0x75 },
  112. { 0x59, 0x01 }, /* AGC dark current compensation: +1 */
  113. { 0x5a, 0x2c },
  114. { 0x5b, 0x0f }, /* AWB chrominance levels */
  115. { 0x5c, 0x10 },
  116. { 0x3d, 0x80 },
  117. { 0x27, 0xa6 },
  118. /* Toggle AWB off and on */
  119. { 0x12, 0x20 },
  120. { 0x12, 0x24 },
  121. { 0xff, 0xff }, /* END MARKER */
  122. };
  123. /* This initializes the OV6x30 camera chip and relevant variables. */
  124. static int ov6x30_init(struct i2c_client *c)
  125. {
  126. struct ovcamchip *ov = i2c_get_clientdata(c);
  127. struct ov6x30 *s;
  128. int rc;
  129. DDEBUG(4, &c->dev, "entered");
  130. rc = ov_write_regvals(c, regvals_init_6x30);
  131. if (rc < 0)
  132. return rc;
  133. ov->spriv = s = kmalloc(sizeof *s, GFP_KERNEL);
  134. if (!s)
  135. return -ENOMEM;
  136. memset(s, 0, sizeof *s);
  137. s->auto_brt = 1;
  138. s->auto_exp = 1;
  139. return rc;
  140. }
  141. static int ov6x30_free(struct i2c_client *c)
  142. {
  143. struct ovcamchip *ov = i2c_get_clientdata(c);
  144. kfree(ov->spriv);
  145. return 0;
  146. }
  147. static int ov6x30_set_control(struct i2c_client *c,
  148. struct ovcamchip_control *ctl)
  149. {
  150. struct ovcamchip *ov = i2c_get_clientdata(c);
  151. struct ov6x30 *s = ov->spriv;
  152. int rc;
  153. int v = ctl->value;
  154. switch (ctl->id) {
  155. case OVCAMCHIP_CID_CONT:
  156. rc = ov_write_mask(c, REG_CNT, v >> 12, 0x0f);
  157. break;
  158. case OVCAMCHIP_CID_BRIGHT:
  159. rc = ov_write(c, REG_BRT, v >> 8);
  160. break;
  161. case OVCAMCHIP_CID_SAT:
  162. rc = ov_write(c, REG_SAT, v >> 8);
  163. break;
  164. case OVCAMCHIP_CID_HUE:
  165. rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
  166. if (rc < 0)
  167. goto out;
  168. rc = ov_write(c, REG_BLUE, v >> 8);
  169. break;
  170. case OVCAMCHIP_CID_EXP:
  171. rc = ov_write(c, REG_EXP, v);
  172. break;
  173. case OVCAMCHIP_CID_FREQ:
  174. {
  175. int sixty = (v == 60);
  176. rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
  177. if (rc < 0)
  178. goto out;
  179. rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
  180. break;
  181. }
  182. case OVCAMCHIP_CID_BANDFILT:
  183. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  184. s->bandfilt = v;
  185. break;
  186. case OVCAMCHIP_CID_AUTOBRIGHT:
  187. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  188. s->auto_brt = v;
  189. break;
  190. case OVCAMCHIP_CID_AUTOEXP:
  191. rc = ov_write_mask(c, 0x28, v?0x00:0x10, 0x10);
  192. s->auto_exp = v;
  193. break;
  194. case OVCAMCHIP_CID_BACKLIGHT:
  195. {
  196. rc = ov_write_mask(c, 0x4e, v?0x80:0x60, 0xe0);
  197. if (rc < 0)
  198. goto out;
  199. rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
  200. if (rc < 0)
  201. goto out;
  202. rc = ov_write_mask(c, 0x28, v?0x02:0x00, 0x02);
  203. s->backlight = v;
  204. break;
  205. }
  206. case OVCAMCHIP_CID_MIRROR:
  207. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  208. s->mirror = v;
  209. break;
  210. default:
  211. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  212. return -EPERM;
  213. }
  214. out:
  215. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  216. return rc;
  217. }
  218. static int ov6x30_get_control(struct i2c_client *c,
  219. struct ovcamchip_control *ctl)
  220. {
  221. struct ovcamchip *ov = i2c_get_clientdata(c);
  222. struct ov6x30 *s = ov->spriv;
  223. int rc = 0;
  224. unsigned char val = 0;
  225. switch (ctl->id) {
  226. case OVCAMCHIP_CID_CONT:
  227. rc = ov_read(c, REG_CNT, &val);
  228. ctl->value = (val & 0x0f) << 12;
  229. break;
  230. case OVCAMCHIP_CID_BRIGHT:
  231. rc = ov_read(c, REG_BRT, &val);
  232. ctl->value = val << 8;
  233. break;
  234. case OVCAMCHIP_CID_SAT:
  235. rc = ov_read(c, REG_SAT, &val);
  236. ctl->value = val << 8;
  237. break;
  238. case OVCAMCHIP_CID_HUE:
  239. rc = ov_read(c, REG_BLUE, &val);
  240. ctl->value = val << 8;
  241. break;
  242. case OVCAMCHIP_CID_EXP:
  243. rc = ov_read(c, REG_EXP, &val);
  244. ctl->value = val;
  245. break;
  246. case OVCAMCHIP_CID_BANDFILT:
  247. ctl->value = s->bandfilt;
  248. break;
  249. case OVCAMCHIP_CID_AUTOBRIGHT:
  250. ctl->value = s->auto_brt;
  251. break;
  252. case OVCAMCHIP_CID_AUTOEXP:
  253. ctl->value = s->auto_exp;
  254. break;
  255. case OVCAMCHIP_CID_BACKLIGHT:
  256. ctl->value = s->backlight;
  257. break;
  258. case OVCAMCHIP_CID_MIRROR:
  259. ctl->value = s->mirror;
  260. break;
  261. default:
  262. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  263. return -EPERM;
  264. }
  265. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  266. return rc;
  267. }
  268. static int ov6x30_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  269. {
  270. /******** QCIF-specific regs ********/
  271. ov_write_mask(c, 0x14, win->quarter?0x20:0x00, 0x20);
  272. /******** Palette-specific regs ********/
  273. if (win->format == VIDEO_PALETTE_GREY) {
  274. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  275. /* Do nothing - we're already in 8-bit mode */
  276. } else {
  277. ov_write_mask(c, 0x13, 0x20, 0x20);
  278. }
  279. } else {
  280. /* The OV518 needs special treatment. Although both the OV518
  281. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  282. * bus is actually used. The UV bus is tied to ground.
  283. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  284. * output mode */
  285. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  286. /* Do nothing - we want to stay in 8-bit mode */
  287. /* Warning: Messing with reg 0x13 breaks OV518 color */
  288. } else {
  289. ov_write_mask(c, 0x13, 0x00, 0x20);
  290. }
  291. }
  292. /******** Clock programming ********/
  293. ov_write(c, 0x11, win->clockdiv);
  294. return 0;
  295. }
  296. static int ov6x30_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  297. {
  298. int ret, hwscale, vwscale;
  299. ret = ov6x30_mode_init(c, win);
  300. if (ret < 0)
  301. return ret;
  302. if (win->quarter) {
  303. hwscale = 0;
  304. vwscale = 0;
  305. } else {
  306. hwscale = 1;
  307. vwscale = 1; /* The datasheet says 0; it's wrong */
  308. }
  309. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  310. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  311. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  312. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  313. return 0;
  314. }
  315. static int ov6x30_command(struct i2c_client *c, unsigned int cmd, void *arg)
  316. {
  317. switch (cmd) {
  318. case OVCAMCHIP_CMD_S_CTRL:
  319. return ov6x30_set_control(c, arg);
  320. case OVCAMCHIP_CMD_G_CTRL:
  321. return ov6x30_get_control(c, arg);
  322. case OVCAMCHIP_CMD_S_MODE:
  323. return ov6x30_set_window(c, arg);
  324. default:
  325. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  326. return -ENOIOCTLCMD;
  327. }
  328. }
  329. struct ovcamchip_ops ov6x30_ops = {
  330. .init = ov6x30_init,
  331. .free = ov6x30_free,
  332. .command = ov6x30_command,
  333. };