ovcamchip_core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. extern struct ovcamchip_ops ov6x20_ops;
  44. extern struct ovcamchip_ops ov6x30_ops;
  45. extern struct ovcamchip_ops ov7x10_ops;
  46. extern struct ovcamchip_ops ov7x20_ops;
  47. extern struct ovcamchip_ops ov76be_ops;
  48. static char *chip_names[NUM_CC_TYPES] = {
  49. [CC_UNKNOWN] = "Unknown chip",
  50. [CC_OV76BE] = "OV76BE",
  51. [CC_OV7610] = "OV7610",
  52. [CC_OV7620] = "OV7620",
  53. [CC_OV7620AE] = "OV7620AE",
  54. [CC_OV6620] = "OV6620",
  55. [CC_OV6630] = "OV6630",
  56. [CC_OV6630AE] = "OV6630AE",
  57. [CC_OV6630AF] = "OV6630AF",
  58. };
  59. /* Forward declarations */
  60. static struct i2c_driver driver;
  61. static struct i2c_client client_template;
  62. /* ----------------------------------------------------------------------- */
  63. int ov_write_regvals(struct i2c_client *c, struct ovcamchip_regvals *rvals)
  64. {
  65. int rc;
  66. while (rvals->reg != 0xff) {
  67. rc = ov_write(c, rvals->reg, rvals->val);
  68. if (rc < 0)
  69. return rc;
  70. rvals++;
  71. }
  72. return 0;
  73. }
  74. /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
  75. * the same position as 1's in "mask" are cleared and set to "value". Bits
  76. * that are in the same position as 0's in "mask" are preserved, regardless
  77. * of their respective state in "value".
  78. */
  79. int ov_write_mask(struct i2c_client *c,
  80. unsigned char reg,
  81. unsigned char value,
  82. unsigned char mask)
  83. {
  84. int rc;
  85. unsigned char oldval, newval;
  86. if (mask == 0xff) {
  87. newval = value;
  88. } else {
  89. rc = ov_read(c, reg, &oldval);
  90. if (rc < 0)
  91. return rc;
  92. oldval &= (~mask); /* Clear the masked bits */
  93. value &= mask; /* Enforce mask on value */
  94. newval = oldval | value; /* Set the desired bits */
  95. }
  96. return ov_write(c, reg, newval);
  97. }
  98. /* ----------------------------------------------------------------------- */
  99. /* Reset the chip and ensure that I2C is synchronized. Returns <0 if failure.
  100. */
  101. static int init_camchip(struct i2c_client *c)
  102. {
  103. int i, success;
  104. unsigned char high, low;
  105. /* Reset the chip */
  106. ov_write(c, 0x12, 0x80);
  107. /* Wait for it to initialize */
  108. msleep(150);
  109. for (i = 0, success = 0; i < I2C_DETECT_RETRIES && !success; i++) {
  110. if (ov_read(c, GENERIC_REG_ID_HIGH, &high) >= 0) {
  111. if (ov_read(c, GENERIC_REG_ID_LOW, &low) >= 0) {
  112. if (high == 0x7F && low == 0xA2) {
  113. success = 1;
  114. continue;
  115. }
  116. }
  117. }
  118. /* Reset the chip */
  119. ov_write(c, 0x12, 0x80);
  120. /* Wait for it to initialize */
  121. msleep(150);
  122. /* Dummy read to sync I2C */
  123. ov_read(c, 0x00, &low);
  124. }
  125. if (!success)
  126. return -EIO;
  127. PDEBUG(1, "I2C synced in %d attempt(s)", i);
  128. return 0;
  129. }
  130. /* This detects the OV7610, OV7620, or OV76BE chip. */
  131. static int ov7xx0_detect(struct i2c_client *c)
  132. {
  133. struct ovcamchip *ov = i2c_get_clientdata(c);
  134. int rc;
  135. unsigned char val;
  136. PDEBUG(4, "");
  137. /* Detect chip (sub)type */
  138. rc = ov_read(c, GENERIC_REG_COM_I, &val);
  139. if (rc < 0) {
  140. PERROR("Error detecting ov7xx0 type");
  141. return rc;
  142. }
  143. if ((val & 3) == 3) {
  144. PINFO("Camera chip is an OV7610");
  145. ov->subtype = CC_OV7610;
  146. } else if ((val & 3) == 1) {
  147. rc = ov_read(c, 0x15, &val);
  148. if (rc < 0) {
  149. PERROR("Error detecting ov7xx0 type");
  150. return rc;
  151. }
  152. if (val & 1) {
  153. PINFO("Camera chip is an OV7620AE");
  154. /* OV7620 is a close enough match for now. There are
  155. * some definite differences though, so this should be
  156. * fixed */
  157. ov->subtype = CC_OV7620;
  158. } else {
  159. PINFO("Camera chip is an OV76BE");
  160. ov->subtype = CC_OV76BE;
  161. }
  162. } else if ((val & 3) == 0) {
  163. PINFO("Camera chip is an OV7620");
  164. ov->subtype = CC_OV7620;
  165. } else {
  166. PERROR("Unknown camera chip version: %d", val & 3);
  167. return -ENOSYS;
  168. }
  169. if (ov->subtype == CC_OV76BE)
  170. ov->sops = &ov76be_ops;
  171. else if (ov->subtype == CC_OV7620)
  172. ov->sops = &ov7x20_ops;
  173. else
  174. ov->sops = &ov7x10_ops;
  175. return 0;
  176. }
  177. /* This detects the OV6620, OV6630, OV6630AE, or OV6630AF chip. */
  178. static int ov6xx0_detect(struct i2c_client *c)
  179. {
  180. struct ovcamchip *ov = i2c_get_clientdata(c);
  181. int rc;
  182. unsigned char val;
  183. PDEBUG(4, "");
  184. /* Detect chip (sub)type */
  185. rc = ov_read(c, GENERIC_REG_COM_I, &val);
  186. if (rc < 0) {
  187. PERROR("Error detecting ov6xx0 type");
  188. return -1;
  189. }
  190. if ((val & 3) == 0) {
  191. ov->subtype = CC_OV6630;
  192. PINFO("Camera chip is an OV6630");
  193. } else if ((val & 3) == 1) {
  194. ov->subtype = CC_OV6620;
  195. PINFO("Camera chip is an OV6620");
  196. } else if ((val & 3) == 2) {
  197. ov->subtype = CC_OV6630;
  198. PINFO("Camera chip is an OV6630AE");
  199. } else if ((val & 3) == 3) {
  200. ov->subtype = CC_OV6630;
  201. PINFO("Camera chip is an OV6630AF");
  202. }
  203. if (ov->subtype == CC_OV6620)
  204. ov->sops = &ov6x20_ops;
  205. else
  206. ov->sops = &ov6x30_ops;
  207. return 0;
  208. }
  209. static int ovcamchip_detect(struct i2c_client *c)
  210. {
  211. /* Ideally we would just try a single register write and see if it NAKs.
  212. * That isn't possible since the OV518 can't report I2C transaction
  213. * failures. So, we have to try to initialize the chip (i.e. reset it
  214. * and check the ID registers) to detect its presence. */
  215. /* Test for 7xx0 */
  216. PDEBUG(3, "Testing for 0V7xx0");
  217. c->addr = OV7xx0_SID;
  218. if (init_camchip(c) < 0) {
  219. /* Test for 6xx0 */
  220. PDEBUG(3, "Testing for 0V6xx0");
  221. c->addr = OV6xx0_SID;
  222. if (init_camchip(c) < 0) {
  223. return -ENODEV;
  224. } else {
  225. if (ov6xx0_detect(c) < 0) {
  226. PERROR("Failed to init OV6xx0");
  227. return -EIO;
  228. }
  229. }
  230. } else {
  231. if (ov7xx0_detect(c) < 0) {
  232. PERROR("Failed to init OV7xx0");
  233. return -EIO;
  234. }
  235. }
  236. return 0;
  237. }
  238. /* ----------------------------------------------------------------------- */
  239. static int ovcamchip_attach(struct i2c_adapter *adap)
  240. {
  241. int rc = 0;
  242. struct ovcamchip *ov;
  243. struct i2c_client *c;
  244. /* I2C is not a PnP bus, so we can never be certain that we're talking
  245. * to the right chip. To prevent damage to EEPROMS and such, only
  246. * attach to adapters that are known to contain OV camera chips. */
  247. switch (adap->id) {
  248. case I2C_HW_SMBUS_OV511:
  249. case I2C_HW_SMBUS_OV518:
  250. case I2C_HW_SMBUS_OVFX2:
  251. case I2C_HW_SMBUS_W9968CF:
  252. PDEBUG(1, "Adapter ID 0x%06x accepted", adap->id);
  253. break;
  254. default:
  255. PDEBUG(1, "Adapter ID 0x%06x rejected", adap->id);
  256. return -ENODEV;
  257. }
  258. c = kmalloc(sizeof *c, GFP_KERNEL);
  259. if (!c) {
  260. rc = -ENOMEM;
  261. goto no_client;
  262. }
  263. memcpy(c, &client_template, sizeof *c);
  264. c->adapter = adap;
  265. strcpy(c->name, "OV????");
  266. ov = kzalloc(sizeof *ov, GFP_KERNEL);
  267. if (!ov) {
  268. rc = -ENOMEM;
  269. goto no_ov;
  270. }
  271. i2c_set_clientdata(c, ov);
  272. rc = ovcamchip_detect(c);
  273. if (rc < 0)
  274. goto error;
  275. strcpy(c->name, chip_names[ov->subtype]);
  276. PDEBUG(1, "Camera chip detection complete");
  277. i2c_attach_client(c);
  278. return rc;
  279. error:
  280. kfree(ov);
  281. no_ov:
  282. kfree(c);
  283. no_client:
  284. PDEBUG(1, "returning %d", rc);
  285. return rc;
  286. }
  287. static int ovcamchip_detach(struct i2c_client *c)
  288. {
  289. struct ovcamchip *ov = i2c_get_clientdata(c);
  290. int rc;
  291. rc = ov->sops->free(c);
  292. if (rc < 0)
  293. return rc;
  294. i2c_detach_client(c);
  295. kfree(ov);
  296. kfree(c);
  297. return 0;
  298. }
  299. static int ovcamchip_command(struct i2c_client *c, unsigned int cmd, void *arg)
  300. {
  301. struct ovcamchip *ov = i2c_get_clientdata(c);
  302. if (!ov->initialized &&
  303. cmd != OVCAMCHIP_CMD_Q_SUBTYPE &&
  304. cmd != OVCAMCHIP_CMD_INITIALIZE) {
  305. dev_err(&c->dev, "ERROR: Camera chip not initialized yet!\n");
  306. return -EPERM;
  307. }
  308. switch (cmd) {
  309. case OVCAMCHIP_CMD_Q_SUBTYPE:
  310. {
  311. *(int *)arg = ov->subtype;
  312. return 0;
  313. }
  314. case OVCAMCHIP_CMD_INITIALIZE:
  315. {
  316. int rc;
  317. if (mono == -1)
  318. ov->mono = *(int *)arg;
  319. else
  320. ov->mono = mono;
  321. if (ov->mono) {
  322. if (ov->subtype != CC_OV7620)
  323. dev_warn(&c->dev, "Warning: Monochrome not "
  324. "implemented for this chip\n");
  325. else
  326. dev_info(&c->dev, "Initializing chip as "
  327. "monochrome\n");
  328. }
  329. rc = ov->sops->init(c);
  330. if (rc < 0)
  331. return rc;
  332. ov->initialized = 1;
  333. return 0;
  334. }
  335. default:
  336. return ov->sops->command(c, cmd, arg);
  337. }
  338. }
  339. /* ----------------------------------------------------------------------- */
  340. static struct i2c_driver driver = {
  341. .driver = {
  342. .name = "ovcamchip",
  343. },
  344. .id = I2C_DRIVERID_OVCAMCHIP,
  345. .class = I2C_CLASS_CAM_DIGITAL,
  346. .attach_adapter = ovcamchip_attach,
  347. .detach_client = ovcamchip_detach,
  348. .command = ovcamchip_command,
  349. };
  350. static struct i2c_client client_template = {
  351. .name = "(unset)",
  352. .driver = &driver,
  353. };
  354. static int __init ovcamchip_init(void)
  355. {
  356. #ifdef DEBUG
  357. ovcamchip_debug = debug;
  358. #endif
  359. PINFO(DRIVER_VERSION " : " DRIVER_DESC);
  360. return i2c_add_driver(&driver);
  361. }
  362. static void __exit ovcamchip_exit(void)
  363. {
  364. i2c_del_driver(&driver);
  365. }
  366. module_init(ovcamchip_init);
  367. module_exit(ovcamchip_exit);