ovcamchip_core.c 10.0 KB

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