ov6x30.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 = kzalloc(sizeof *s, GFP_KERNEL);
  134. if (!s)
  135. return -ENOMEM;
  136. s->auto_brt = 1;
  137. s->auto_exp = 1;
  138. return rc;
  139. }
  140. static int ov6x30_free(struct i2c_client *c)
  141. {
  142. struct ovcamchip *ov = i2c_get_clientdata(c);
  143. kfree(ov->spriv);
  144. return 0;
  145. }
  146. static int ov6x30_set_control(struct i2c_client *c,
  147. struct ovcamchip_control *ctl)
  148. {
  149. struct ovcamchip *ov = i2c_get_clientdata(c);
  150. struct ov6x30 *s = ov->spriv;
  151. int rc;
  152. int v = ctl->value;
  153. switch (ctl->id) {
  154. case OVCAMCHIP_CID_CONT:
  155. rc = ov_write_mask(c, REG_CNT, v >> 12, 0x0f);
  156. break;
  157. case OVCAMCHIP_CID_BRIGHT:
  158. rc = ov_write(c, REG_BRT, v >> 8);
  159. break;
  160. case OVCAMCHIP_CID_SAT:
  161. rc = ov_write(c, REG_SAT, v >> 8);
  162. break;
  163. case OVCAMCHIP_CID_HUE:
  164. rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
  165. if (rc < 0)
  166. goto out;
  167. rc = ov_write(c, REG_BLUE, v >> 8);
  168. break;
  169. case OVCAMCHIP_CID_EXP:
  170. rc = ov_write(c, REG_EXP, v);
  171. break;
  172. case OVCAMCHIP_CID_FREQ:
  173. {
  174. int sixty = (v == 60);
  175. rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
  176. if (rc < 0)
  177. goto out;
  178. rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
  179. break;
  180. }
  181. case OVCAMCHIP_CID_BANDFILT:
  182. rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
  183. s->bandfilt = v;
  184. break;
  185. case OVCAMCHIP_CID_AUTOBRIGHT:
  186. rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
  187. s->auto_brt = v;
  188. break;
  189. case OVCAMCHIP_CID_AUTOEXP:
  190. rc = ov_write_mask(c, 0x28, v?0x00:0x10, 0x10);
  191. s->auto_exp = v;
  192. break;
  193. case OVCAMCHIP_CID_BACKLIGHT:
  194. {
  195. rc = ov_write_mask(c, 0x4e, v?0x80:0x60, 0xe0);
  196. if (rc < 0)
  197. goto out;
  198. rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
  199. if (rc < 0)
  200. goto out;
  201. rc = ov_write_mask(c, 0x28, v?0x02:0x00, 0x02);
  202. s->backlight = v;
  203. break;
  204. }
  205. case OVCAMCHIP_CID_MIRROR:
  206. rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
  207. s->mirror = v;
  208. break;
  209. default:
  210. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  211. return -EPERM;
  212. }
  213. out:
  214. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
  215. return rc;
  216. }
  217. static int ov6x30_get_control(struct i2c_client *c,
  218. struct ovcamchip_control *ctl)
  219. {
  220. struct ovcamchip *ov = i2c_get_clientdata(c);
  221. struct ov6x30 *s = ov->spriv;
  222. int rc = 0;
  223. unsigned char val = 0;
  224. switch (ctl->id) {
  225. case OVCAMCHIP_CID_CONT:
  226. rc = ov_read(c, REG_CNT, &val);
  227. ctl->value = (val & 0x0f) << 12;
  228. break;
  229. case OVCAMCHIP_CID_BRIGHT:
  230. rc = ov_read(c, REG_BRT, &val);
  231. ctl->value = val << 8;
  232. break;
  233. case OVCAMCHIP_CID_SAT:
  234. rc = ov_read(c, REG_SAT, &val);
  235. ctl->value = val << 8;
  236. break;
  237. case OVCAMCHIP_CID_HUE:
  238. rc = ov_read(c, REG_BLUE, &val);
  239. ctl->value = val << 8;
  240. break;
  241. case OVCAMCHIP_CID_EXP:
  242. rc = ov_read(c, REG_EXP, &val);
  243. ctl->value = val;
  244. break;
  245. case OVCAMCHIP_CID_BANDFILT:
  246. ctl->value = s->bandfilt;
  247. break;
  248. case OVCAMCHIP_CID_AUTOBRIGHT:
  249. ctl->value = s->auto_brt;
  250. break;
  251. case OVCAMCHIP_CID_AUTOEXP:
  252. ctl->value = s->auto_exp;
  253. break;
  254. case OVCAMCHIP_CID_BACKLIGHT:
  255. ctl->value = s->backlight;
  256. break;
  257. case OVCAMCHIP_CID_MIRROR:
  258. ctl->value = s->mirror;
  259. break;
  260. default:
  261. DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
  262. return -EPERM;
  263. }
  264. DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
  265. return rc;
  266. }
  267. static int ov6x30_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
  268. {
  269. /******** QCIF-specific regs ********/
  270. ov_write_mask(c, 0x14, win->quarter?0x20:0x00, 0x20);
  271. /******** Palette-specific regs ********/
  272. if (win->format == VIDEO_PALETTE_GREY) {
  273. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  274. /* Do nothing - we're already in 8-bit mode */
  275. } else {
  276. ov_write_mask(c, 0x13, 0x20, 0x20);
  277. }
  278. } else {
  279. /* The OV518 needs special treatment. Although both the OV518
  280. * and the OV6630 support a 16-bit video bus, only the 8 bit Y
  281. * bus is actually used. The UV bus is tied to ground.
  282. * Therefore, the OV6630 needs to be in 8-bit multiplexed
  283. * output mode */
  284. if (c->adapter->id == I2C_HW_SMBUS_OV518) {
  285. /* Do nothing - we want to stay in 8-bit mode */
  286. /* Warning: Messing with reg 0x13 breaks OV518 color */
  287. } else {
  288. ov_write_mask(c, 0x13, 0x00, 0x20);
  289. }
  290. }
  291. /******** Clock programming ********/
  292. ov_write(c, 0x11, win->clockdiv);
  293. return 0;
  294. }
  295. static int ov6x30_set_window(struct i2c_client *c, struct ovcamchip_window *win)
  296. {
  297. int ret, hwscale, vwscale;
  298. ret = ov6x30_mode_init(c, win);
  299. if (ret < 0)
  300. return ret;
  301. if (win->quarter) {
  302. hwscale = 0;
  303. vwscale = 0;
  304. } else {
  305. hwscale = 1;
  306. vwscale = 1; /* The datasheet says 0; it's wrong */
  307. }
  308. ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
  309. ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
  310. ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
  311. ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
  312. return 0;
  313. }
  314. static int ov6x30_command(struct i2c_client *c, unsigned int cmd, void *arg)
  315. {
  316. switch (cmd) {
  317. case OVCAMCHIP_CMD_S_CTRL:
  318. return ov6x30_set_control(c, arg);
  319. case OVCAMCHIP_CMD_G_CTRL:
  320. return ov6x30_get_control(c, arg);
  321. case OVCAMCHIP_CMD_S_MODE:
  322. return ov6x30_set_window(c, arg);
  323. default:
  324. DDEBUG(2, &c->dev, "command not supported: %d", cmd);
  325. return -ENOIOCTLCMD;
  326. }
  327. }
  328. struct ovcamchip_ops ov6x30_ops = {
  329. .init = ov6x30_init,
  330. .free = ov6x30_free,
  331. .command = ov6x30_command,
  332. };