ovcamchip_core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 "ovcamchip_priv.h"
  17. #define DRIVER_VERSION "v2.27 for Linux 2.6"
  18. #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org>"
  19. #define DRIVER_DESC "OV camera chip I2C driver"
  20. #define PINFO(fmt, args...) printk(KERN_INFO "ovcamchip: " fmt "\n" , ## args);
  21. #define PERROR(fmt, args...) printk(KERN_ERR "ovcamchip: " fmt "\n" , ## args);
  22. #ifdef DEBUG
  23. int ovcamchip_debug = 0;
  24. static int debug;
  25. module_param(debug, int, 0);
  26. MODULE_PARM_DESC(debug,
  27. "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=all");
  28. #endif
  29. /* By default, let bridge driver tell us if chip is monochrome. mono=0
  30. * will ignore that and always treat chips as color. mono=1 will force
  31. * monochrome mode for all chips. */
  32. static int mono = -1;
  33. module_param(mono, int, 0);
  34. MODULE_PARM_DESC(mono,
  35. "1=chips are monochrome (OVx1xx), 0=force color, -1=autodetect (default)");
  36. MODULE_AUTHOR(DRIVER_AUTHOR);
  37. MODULE_DESCRIPTION(DRIVER_DESC);
  38. MODULE_LICENSE("GPL");
  39. /* Registers common to all chips, that are needed for detection */
  40. #define GENERIC_REG_ID_HIGH 0x1C /* manufacturer ID MSB */
  41. #define GENERIC_REG_ID_LOW 0x1D /* manufacturer ID LSB */
  42. #define GENERIC_REG_COM_I 0x29 /* misc ID bits */
  43. static char *chip_names[NUM_CC_TYPES] = {
  44. [CC_UNKNOWN] = "Unknown chip",
  45. [CC_OV76BE] = "OV76BE",
  46. [CC_OV7610] = "OV7610",
  47. [CC_OV7620] = "OV7620",
  48. [CC_OV7620AE] = "OV7620AE",
  49. [CC_OV6620] = "OV6620",
  50. [CC_OV6630] = "OV6630",
  51. [CC_OV6630AE] = "OV6630AE",
  52. [CC_OV6630AF] = "OV6630AF",
  53. };
  54. /* Forward declarations */
  55. static struct i2c_driver driver;
  56. static struct i2c_client client_template;
  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. c->addr = OV7xx0_SID;
  213. if (init_camchip(c) < 0) {
  214. /* Test for 6xx0 */
  215. PDEBUG(3, "Testing for 0V6xx0");
  216. c->addr = OV6xx0_SID;
  217. if (init_camchip(c) < 0) {
  218. return -ENODEV;
  219. } else {
  220. if (ov6xx0_detect(c) < 0) {
  221. PERROR("Failed to init OV6xx0");
  222. return -EIO;
  223. }
  224. }
  225. } else {
  226. if (ov7xx0_detect(c) < 0) {
  227. PERROR("Failed to init OV7xx0");
  228. return -EIO;
  229. }
  230. }
  231. return 0;
  232. }
  233. /* ----------------------------------------------------------------------- */
  234. static int ovcamchip_attach(struct i2c_adapter *adap)
  235. {
  236. int rc = 0;
  237. struct ovcamchip *ov;
  238. struct i2c_client *c;
  239. /* I2C is not a PnP bus, so we can never be certain that we're talking
  240. * to the right chip. To prevent damage to EEPROMS and such, only
  241. * attach to adapters that are known to contain OV camera chips. */
  242. switch (adap->id) {
  243. case I2C_HW_SMBUS_OV511:
  244. case I2C_HW_SMBUS_OV518:
  245. case I2C_HW_SMBUS_W9968CF:
  246. PDEBUG(1, "Adapter ID 0x%06x accepted", adap->id);
  247. break;
  248. default:
  249. PDEBUG(1, "Adapter ID 0x%06x rejected", adap->id);
  250. return -ENODEV;
  251. }
  252. c = kmalloc(sizeof *c, GFP_KERNEL);
  253. if (!c) {
  254. rc = -ENOMEM;
  255. goto no_client;
  256. }
  257. memcpy(c, &client_template, sizeof *c);
  258. c->adapter = adap;
  259. strcpy(c->name, "OV????");
  260. ov = kzalloc(sizeof *ov, GFP_KERNEL);
  261. if (!ov) {
  262. rc = -ENOMEM;
  263. goto no_ov;
  264. }
  265. i2c_set_clientdata(c, ov);
  266. rc = ovcamchip_detect(c);
  267. if (rc < 0)
  268. goto error;
  269. strcpy(c->name, chip_names[ov->subtype]);
  270. PDEBUG(1, "Camera chip detection complete");
  271. i2c_attach_client(c);
  272. return rc;
  273. error:
  274. kfree(ov);
  275. no_ov:
  276. kfree(c);
  277. no_client:
  278. PDEBUG(1, "returning %d", rc);
  279. return rc;
  280. }
  281. static int ovcamchip_detach(struct i2c_client *c)
  282. {
  283. struct ovcamchip *ov = i2c_get_clientdata(c);
  284. int rc;
  285. rc = ov->sops->free(c);
  286. if (rc < 0)
  287. return rc;
  288. i2c_detach_client(c);
  289. kfree(ov);
  290. kfree(c);
  291. return 0;
  292. }
  293. static int ovcamchip_command(struct i2c_client *c, unsigned int cmd, void *arg)
  294. {
  295. struct ovcamchip *ov = i2c_get_clientdata(c);
  296. if (!ov->initialized &&
  297. cmd != OVCAMCHIP_CMD_Q_SUBTYPE &&
  298. cmd != OVCAMCHIP_CMD_INITIALIZE) {
  299. dev_err(&c->dev, "ERROR: Camera chip not initialized yet!\n");
  300. return -EPERM;
  301. }
  302. switch (cmd) {
  303. case OVCAMCHIP_CMD_Q_SUBTYPE:
  304. {
  305. *(int *)arg = ov->subtype;
  306. return 0;
  307. }
  308. case OVCAMCHIP_CMD_INITIALIZE:
  309. {
  310. int rc;
  311. if (mono == -1)
  312. ov->mono = *(int *)arg;
  313. else
  314. ov->mono = mono;
  315. if (ov->mono) {
  316. if (ov->subtype != CC_OV7620)
  317. dev_warn(&c->dev, "Warning: Monochrome not "
  318. "implemented for this chip\n");
  319. else
  320. dev_info(&c->dev, "Initializing chip as "
  321. "monochrome\n");
  322. }
  323. rc = ov->sops->init(c);
  324. if (rc < 0)
  325. return rc;
  326. ov->initialized = 1;
  327. return 0;
  328. }
  329. default:
  330. return ov->sops->command(c, cmd, arg);
  331. }
  332. }
  333. /* ----------------------------------------------------------------------- */
  334. static struct i2c_driver driver = {
  335. .driver = {
  336. .name = "ovcamchip",
  337. },
  338. .id = I2C_DRIVERID_OVCAMCHIP,
  339. .attach_adapter = ovcamchip_attach,
  340. .detach_client = ovcamchip_detach,
  341. .command = ovcamchip_command,
  342. };
  343. static struct i2c_client client_template = {
  344. .name = "(unset)",
  345. .driver = &driver,
  346. };
  347. static int __init ovcamchip_init(void)
  348. {
  349. #ifdef DEBUG
  350. ovcamchip_debug = debug;
  351. #endif
  352. PINFO(DRIVER_VERSION " : " DRIVER_DESC);
  353. return i2c_add_driver(&driver);
  354. }
  355. static void __exit ovcamchip_exit(void)
  356. {
  357. i2c_del_driver(&driver);
  358. }
  359. module_init(ovcamchip_init);
  360. module_exit(ovcamchip_exit);