ovcamchip_core.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* Shared Code for OmniVision Camera Chip Drivers
  2. *
  3. * Copyright (c) 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/init.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/i2c.h>
  17. #include <media/v4l2-device.h>
  18. #include <media/v4l2-i2c-drv.h>
  19. #include "ovcamchip_priv.h"
  20. #define DRIVER_VERSION "v2.27 for Linux 2.6"
  21. #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org>"
  22. #define DRIVER_DESC "OV camera chip I2C driver"
  23. #define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
  24. #define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
  25. #ifdef DEBUG
  26. int ovcamchip_debug = 0;
  27. static int debug;
  28. module_param(debug, int, 0);
  29. MODULE_PARM_DESC(debug,
  30. "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=all");
  31. #endif
  32. /* By default, let bridge driver tell us if chip is monochrome. mono=0
  33. * will ignore that and always treat chips as color. mono=1 will force
  34. * monochrome mode for all chips. */
  35. static int mono = -1;
  36. module_param(mono, int, 0);
  37. MODULE_PARM_DESC(mono,
  38. "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
  39. MODULE_AUTHOR(DRIVER_AUTHOR);
  40. MODULE_DESCRIPTION(DRIVER_DESC);
  41. MODULE_LICENSE("GPL");
  42. /* Registers common to all chips, that are needed for detection */
  43. #define GENERIC_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
  44. #define GENERIC_REG_ID_LOW 0x1D /* manufacturer ID LSB */
  45. #define GENERIC_REG_COM_I 0x29 /* misc ID bits */
  46. static char *chip_names[NUM_CC_TYPES] = {
  47. [CC_UNKNOWN] = "Unknown chip",
  48. [CC_OV76BE] = "OV76BE",
  49. [CC_OV7610] = "OV7610",
  50. [CC_OV7620] = "OV7620",
  51. [CC_OV7620AE] = "OV7620AE",
  52. [CC_OV6620] = "OV6620",
  53. [CC_OV6630] = "OV6630",
  54. [CC_OV6630AE] = "OV6630AE",
  55. [CC_OV6630AF] = "OV6630AF",
  56. };
  57. /* ----------------------------------------------------------------------- */
  58. int ov_write_regvals(struct i2c_client *c, struct ovcamchip_regvals *rvals)
  59. {
  60. int rc;
  61. while (rvals->reg != 0xff) {
  62. rc = ov_write(c, rvals->reg, rvals->val);
  63. if (rc < 0)
  64. return rc;
  65. rvals++;
  66. }
  67. return 0;
  68. }
  69. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  70. * the same position as 1's in "mask" are cleared and set to "value". Bits
  71. * that are in the same position as 0's in "mask" are preserved, regardless
  72. * of their respective state in "value".
  73. */
  74. int ov_write_mask(struct i2c_client *c,
  75. unsigned char reg,
  76. unsigned char value,
  77. unsigned char mask)
  78. {
  79. int rc;
  80. unsigned char oldval, newval;
  81. if (mask == 0xff) {
  82. newval = value;
  83. } else {
  84. rc = ov_read(c, reg, &oldval);
  85. if (rc < 0)
  86. return rc;
  87. oldval &= (~mask); /* Clear the masked bits */
  88. value &= mask; /* Enforce mask on value */
  89. newval = oldval | value; /* Set the desired bits */
  90. }
  91. return ov_write(c, reg, newval);
  92. }
  93. /* ----------------------------------------------------------------------- */
  94. /* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
  95. */
  96. static int init_camchip(struct i2c_client *c)
  97. {
  98. int i, success;
  99. unsigned char high, low;
  100. /* Reset the chip */
  101. ov_write(c, 0x12, 0x80);
  102. /* Wait for it to initialize */
  103. msleep(150);
  104. for (i = 0, success = 0; i < I2C_DETECT_RETRIES && !success; i++) {
  105. if (ov_read(c, GENERIC_REG_ID_HIGH, &high) >= 0) {
  106. if (ov_read(c, GENERIC_REG_ID_LOW, &low) >= 0) {
  107. if (high == 0x7F && low == 0xA2) {
  108. success = 1;
  109. continue;
  110. }
  111. }
  112. }
  113. /* Reset the chip */
  114. ov_write(c, 0x12, 0x80);
  115. /* Wait for it to initialize */
  116. msleep(150);
  117. /* Dummy read to sync I2C */
  118. ov_read(c, 0x00, &low);
  119. }
  120. if (!success)
  121. return -EIO;
  122. PDEBUG(1, "I2C synced in %d attempt(s)", i);
  123. return 0;
  124. }
  125. /* This detects the OV7610, OV7620, or OV76BE chip. */
  126. static int ov7xx0_detect(struct i2c_client *c)
  127. {
  128. struct ovcamchip *ov = i2c_get_clientdata(c);
  129. int rc;
  130. unsigned char val;
  131. PDEBUG(4, "");
  132. /* Detect chip (sub)type */
  133. rc = ov_read(c, GENERIC_REG_COM_I, &val);
  134. if (rc < 0) {
  135. PERROR("Error detecting ov7xx0 type");
  136. return rc;
  137. }
  138. if ((val & 3) == 3) {
  139. PINFO("Camera chip is an OV7610");
  140. ov->subtype = CC_OV7610;
  141. } else if ((val & 3) == 1) {
  142. rc = ov_read(c, 0x15, &val);
  143. if (rc < 0) {
  144. PERROR("Error detecting ov7xx0 type");
  145. return rc;
  146. }
  147. if (val & 1) {
  148. PINFO("Camera chip is an OV7620AE");
  149. /* OV7620 is a close enough match for now. There are
  150. * some definite differences though, so this should be
  151. * fixed */
  152. ov->subtype = CC_OV7620;
  153. } else {
  154. PINFO("Camera chip is an OV76BE");
  155. ov->subtype = CC_OV76BE;
  156. }
  157. } else if ((val & 3) == 0) {
  158. PINFO("Camera chip is an OV7620");
  159. ov->subtype = CC_OV7620;
  160. } else {
  161. PERROR("Unknown camera chip version: %d", val & 3);
  162. return -ENOSYS;
  163. }
  164. if (ov->subtype == CC_OV76BE)
  165. ov->sops = &ov76be_ops;
  166. else if (ov->subtype == CC_OV7620)
  167. ov->sops = &ov7x20_ops;
  168. else
  169. ov->sops = &ov7x10_ops;
  170. return 0;
  171. }
  172. /* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
  173. static int ov6xx0_detect(struct i2c_client *c)
  174. {
  175. struct ovcamchip *ov = i2c_get_clientdata(c);
  176. int rc;
  177. unsigned char val;
  178. PDEBUG(4, "");
  179. /* Detect chip (sub)type */
  180. rc = ov_read(c, GENERIC_REG_COM_I, &val);
  181. if (rc < 0) {
  182. PERROR("Error detecting ov6xx0 type");
  183. return -1;
  184. }
  185. if ((val & 3) == 0) {
  186. ov->subtype = CC_OV6630;
  187. PINFO("Camera chip is an OV6630");
  188. } else if ((val & 3) == 1) {
  189. ov->subtype = CC_OV6620;
  190. PINFO("Camera chip is an OV6620");
  191. } else if ((val & 3) == 2) {
  192. ov->subtype = CC_OV6630;
  193. PINFO("Camera chip is an OV6630AE");
  194. } else if ((val & 3) == 3) {
  195. ov->subtype = CC_OV6630;
  196. PINFO("Camera chip is an OV6630AF");
  197. }
  198. if (ov->subtype == CC_OV6620)
  199. ov->sops = &ov6x20_ops;
  200. else
  201. ov->sops = &ov6x30_ops;
  202. return 0;
  203. }
  204. static int ovcamchip_detect(struct i2c_client *c)
  205. {
  206. /* Ideally we would just try a single register write and see if it NAKs.
  207. * That isn't possible since the OV518 can't report I2C transaction
  208. * failures. So, we have to try to initialize the chip (i.e. reset it
  209. * and check the ID registers) to detect its presence. */
  210. /* Test for 7xx0 */
  211. PDEBUG(3, "Testing for 0V7xx0");
  212. if (init_camchip(c) < 0)
  213. return -ENODEV;
  214. /* 7-bit addresses with bit 0 set are for the OV7xx0 */
  215. if (c->addr & 1) {
  216. if (ov7xx0_detect(c) < 0) {
  217. PERROR("Failed to init OV7xx0");
  218. return -EIO;
  219. }
  220. return 0;
  221. }
  222. /* Test for 6xx0 */
  223. PDEBUG(3, "Testing for 0V6xx0");
  224. if (ov6xx0_detect(c) < 0) {
  225. PERROR("Failed to init OV6xx0");
  226. return -EIO;
  227. }
  228. return 0;
  229. }
  230. /* ----------------------------------------------------------------------- */
  231. static long ovcamchip_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
  232. {
  233. struct ovcamchip *ov = to_ovcamchip(sd);
  234. struct i2c_client *c = v4l2_get_subdevdata(sd);
  235. if (!ov->initialized &&
  236. cmd != OVCAMCHIP_CMD_Q_SUBTYPE &&
  237. cmd != OVCAMCHIP_CMD_INITIALIZE) {
  238. v4l2_err(sd, "Camera chip not initialized yet!\n");
  239. return -EPERM;
  240. }
  241. switch (cmd) {
  242. case OVCAMCHIP_CMD_Q_SUBTYPE:
  243. {
  244. *(int *)arg = ov->subtype;
  245. return 0;
  246. }
  247. case OVCAMCHIP_CMD_INITIALIZE:
  248. {
  249. int rc;
  250. if (mono == -1)
  251. ov->mono = *(int *)arg;
  252. else
  253. ov->mono = mono;
  254. if (ov->mono) {
  255. if (ov->subtype != CC_OV7620)
  256. v4l2_warn(sd, "Monochrome not "
  257. "implemented for this chip\n");
  258. else
  259. v4l2_info(sd, "Initializing chip as "
  260. "monochrome\n");
  261. }
  262. rc = ov->sops->init(c);
  263. if (rc < 0)
  264. return rc;
  265. ov->initialized = 1;
  266. return 0;
  267. }
  268. default:
  269. return ov->sops->command(c, cmd, arg);
  270. }
  271. }
  272. /* ----------------------------------------------------------------------- */
  273. static const struct v4l2_subdev_core_ops ovcamchip_core_ops = {
  274. .ioctl = ovcamchip_ioctl,
  275. };
  276. static const struct v4l2_subdev_ops ovcamchip_ops = {
  277. .core = &ovcamchip_core_ops,
  278. };
  279. static int ovcamchip_probe(struct i2c_client *client,
  280. const struct i2c_device_id *id)
  281. {
  282. struct ovcamchip *ov;
  283. struct v4l2_subdev *sd;
  284. int rc = 0;
  285. ov = kzalloc(sizeof *ov, GFP_KERNEL);
  286. if (!ov) {
  287. rc = -ENOMEM;
  288. goto no_ov;
  289. }
  290. sd = &ov->sd;
  291. v4l2_i2c_subdev_init(sd, client, &ovcamchip_ops);
  292. rc = ovcamchip_detect(client);
  293. if (rc < 0)
  294. goto error;
  295. v4l_info(client, "%s found @ 0x%02x (%s)\n",
  296. chip_names[ov->subtype], client->addr << 1, client->adapter->name);
  297. PDEBUG(1, "Camera chip detection complete");
  298. return rc;
  299. error:
  300. kfree(ov);
  301. no_ov:
  302. PDEBUG(1, "returning %d", rc);
  303. return rc;
  304. }
  305. static int ovcamchip_remove(struct i2c_client *client)
  306. {
  307. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  308. struct ovcamchip *ov = to_ovcamchip(sd);
  309. int rc;
  310. v4l2_device_unregister_subdev(sd);
  311. rc = ov->sops->free(client);
  312. if (rc < 0)
  313. return rc;
  314. kfree(ov);
  315. return 0;
  316. }
  317. /* ----------------------------------------------------------------------- */
  318. static const struct i2c_device_id ovcamchip_id[] = {
  319. { "ovcamchip", 0 },
  320. { }
  321. };
  322. MODULE_DEVICE_TABLE(i2c, ovcamchip_id);
  323. static struct v4l2_i2c_driver_data v4l2_i2c_data = {
  324. .name = "ovcamchip",
  325. .probe = ovcamchip_probe,
  326. .remove = ovcamchip_remove,
  327. .id_table = ovcamchip_id,
  328. };