cafe-driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * A driver for the CMOS camera controller in the Marvell 88ALP01 "cafe"
  3. * multifunction chip. Currently works with the Omnivision OV7670
  4. * sensor.
  5. *
  6. * The data sheet for this device can be found at:
  7. * http://www.marvell.com/products/pc_connectivity/88alp01/
  8. *
  9. * Copyright 2006-11 One Laptop Per Child Association, Inc.
  10. * Copyright 2006-11 Jonathan Corbet <corbet@lwn.net>
  11. *
  12. * Written by Jonathan Corbet, corbet@lwn.net.
  13. *
  14. * v4l2_device/v4l2_subdev conversion by:
  15. * Copyright (C) 2009 Hans Verkuil <hverkuil@xs4all.nl>
  16. *
  17. * Note: this conversion is untested! Please contact the linux-media
  18. * mailinglist if you can test this, together with the test results.
  19. *
  20. * This file may be distributed under the terms of the GNU General
  21. * Public License, version 2.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/i2c.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/slab.h>
  31. #include <linux/videodev2.h>
  32. #include <media/v4l2-device.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <linux/device.h>
  35. #include <linux/wait.h>
  36. #include <linux/delay.h>
  37. #include <linux/io.h>
  38. #include "mcam-core.h"
  39. #define CAFE_VERSION 0x000002
  40. /*
  41. * Parameters.
  42. */
  43. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  44. MODULE_DESCRIPTION("Marvell 88ALP01 CMOS Camera Controller driver");
  45. MODULE_LICENSE("GPL");
  46. MODULE_SUPPORTED_DEVICE("Video");
  47. struct cafe_camera {
  48. int registered; /* Fully initialized? */
  49. struct mcam_camera mcam;
  50. struct pci_dev *pdev;
  51. wait_queue_head_t smbus_wait; /* Waiting on i2c events */
  52. };
  53. /*
  54. * Debugging and related.
  55. */
  56. #define cam_err(cam, fmt, arg...) \
  57. dev_err(&(cam)->pdev->dev, fmt, ##arg);
  58. #define cam_warn(cam, fmt, arg...) \
  59. dev_warn(&(cam)->pdev->dev, fmt, ##arg);
  60. /* -------------------------------------------------------------------- */
  61. /*
  62. * The I2C/SMBUS interface to the camera itself starts here. The
  63. * controller handles SMBUS itself, presenting a relatively simple register
  64. * interface; all we have to do is to tell it where to route the data.
  65. */
  66. #define CAFE_SMBUS_TIMEOUT (HZ) /* generous */
  67. static inline struct cafe_camera *to_cam(struct v4l2_device *dev)
  68. {
  69. struct mcam_camera *m = container_of(dev, struct mcam_camera, v4l2_dev);
  70. return container_of(m, struct cafe_camera, mcam);
  71. }
  72. static int cafe_smbus_write_done(struct mcam_camera *mcam)
  73. {
  74. unsigned long flags;
  75. int c1;
  76. /*
  77. * We must delay after the interrupt, or the controller gets confused
  78. * and never does give us good status. Fortunately, we don't do this
  79. * often.
  80. */
  81. udelay(20);
  82. spin_lock_irqsave(&mcam->dev_lock, flags);
  83. c1 = mcam_reg_read(mcam, REG_TWSIC1);
  84. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  85. return (c1 & (TWSIC1_WSTAT|TWSIC1_ERROR)) != TWSIC1_WSTAT;
  86. }
  87. static int cafe_smbus_write_data(struct cafe_camera *cam,
  88. u16 addr, u8 command, u8 value)
  89. {
  90. unsigned int rval;
  91. unsigned long flags;
  92. struct mcam_camera *mcam = &cam->mcam;
  93. spin_lock_irqsave(&mcam->dev_lock, flags);
  94. rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
  95. rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
  96. /*
  97. * Marvell sez set clkdiv to all 1's for now.
  98. */
  99. rval |= TWSIC0_CLKDIV;
  100. mcam_reg_write(mcam, REG_TWSIC0, rval);
  101. (void) mcam_reg_read(mcam, REG_TWSIC1); /* force write */
  102. rval = value | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
  103. mcam_reg_write(mcam, REG_TWSIC1, rval);
  104. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  105. /* Unfortunately, reading TWSIC1 too soon after sending a command
  106. * causes the device to die.
  107. * Use a busy-wait because we often send a large quantity of small
  108. * commands at-once; using msleep() would cause a lot of context
  109. * switches which take longer than 2ms, resulting in a noticeable
  110. * boot-time and capture-start delays.
  111. */
  112. mdelay(2);
  113. /*
  114. * Another sad fact is that sometimes, commands silently complete but
  115. * cafe_smbus_write_done() never becomes aware of this.
  116. * This happens at random and appears to possible occur with any
  117. * command.
  118. * We don't understand why this is. We work around this issue
  119. * with the timeout in the wait below, assuming that all commands
  120. * complete within the timeout.
  121. */
  122. wait_event_timeout(cam->smbus_wait, cafe_smbus_write_done(mcam),
  123. CAFE_SMBUS_TIMEOUT);
  124. spin_lock_irqsave(&mcam->dev_lock, flags);
  125. rval = mcam_reg_read(mcam, REG_TWSIC1);
  126. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  127. if (rval & TWSIC1_WSTAT) {
  128. cam_err(cam, "SMBUS write (%02x/%02x/%02x) timed out\n", addr,
  129. command, value);
  130. return -EIO;
  131. }
  132. if (rval & TWSIC1_ERROR) {
  133. cam_err(cam, "SMBUS write (%02x/%02x/%02x) error\n", addr,
  134. command, value);
  135. return -EIO;
  136. }
  137. return 0;
  138. }
  139. static int cafe_smbus_read_done(struct mcam_camera *mcam)
  140. {
  141. unsigned long flags;
  142. int c1;
  143. /*
  144. * We must delay after the interrupt, or the controller gets confused
  145. * and never does give us good status. Fortunately, we don't do this
  146. * often.
  147. */
  148. udelay(20);
  149. spin_lock_irqsave(&mcam->dev_lock, flags);
  150. c1 = mcam_reg_read(mcam, REG_TWSIC1);
  151. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  152. return c1 & (TWSIC1_RVALID|TWSIC1_ERROR);
  153. }
  154. static int cafe_smbus_read_data(struct cafe_camera *cam,
  155. u16 addr, u8 command, u8 *value)
  156. {
  157. unsigned int rval;
  158. unsigned long flags;
  159. struct mcam_camera *mcam = &cam->mcam;
  160. spin_lock_irqsave(&mcam->dev_lock, flags);
  161. rval = TWSIC0_EN | ((addr << TWSIC0_SID_SHIFT) & TWSIC0_SID);
  162. rval |= TWSIC0_OVMAGIC; /* Make OV sensors work */
  163. /*
  164. * Marvel sez set clkdiv to all 1's for now.
  165. */
  166. rval |= TWSIC0_CLKDIV;
  167. mcam_reg_write(mcam, REG_TWSIC0, rval);
  168. (void) mcam_reg_read(mcam, REG_TWSIC1); /* force write */
  169. rval = TWSIC1_READ | ((command << TWSIC1_ADDR_SHIFT) & TWSIC1_ADDR);
  170. mcam_reg_write(mcam, REG_TWSIC1, rval);
  171. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  172. wait_event_timeout(cam->smbus_wait,
  173. cafe_smbus_read_done(mcam), CAFE_SMBUS_TIMEOUT);
  174. spin_lock_irqsave(&mcam->dev_lock, flags);
  175. rval = mcam_reg_read(mcam, REG_TWSIC1);
  176. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  177. if (rval & TWSIC1_ERROR) {
  178. cam_err(cam, "SMBUS read (%02x/%02x) error\n", addr, command);
  179. return -EIO;
  180. }
  181. if (!(rval & TWSIC1_RVALID)) {
  182. cam_err(cam, "SMBUS read (%02x/%02x) timed out\n", addr,
  183. command);
  184. return -EIO;
  185. }
  186. *value = rval & 0xff;
  187. return 0;
  188. }
  189. /*
  190. * Perform a transfer over SMBUS. This thing is called under
  191. * the i2c bus lock, so we shouldn't race with ourselves...
  192. */
  193. static int cafe_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
  194. unsigned short flags, char rw, u8 command,
  195. int size, union i2c_smbus_data *data)
  196. {
  197. struct cafe_camera *cam = i2c_get_adapdata(adapter);
  198. int ret = -EINVAL;
  199. /*
  200. * This interface would appear to only do byte data ops. OK
  201. * it can do word too, but the cam chip has no use for that.
  202. */
  203. if (size != I2C_SMBUS_BYTE_DATA) {
  204. cam_err(cam, "funky xfer size %d\n", size);
  205. return -EINVAL;
  206. }
  207. if (rw == I2C_SMBUS_WRITE)
  208. ret = cafe_smbus_write_data(cam, addr, command, data->byte);
  209. else if (rw == I2C_SMBUS_READ)
  210. ret = cafe_smbus_read_data(cam, addr, command, &data->byte);
  211. return ret;
  212. }
  213. static void cafe_smbus_enable_irq(struct cafe_camera *cam)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&cam->mcam.dev_lock, flags);
  217. mcam_reg_set_bit(&cam->mcam, REG_IRQMASK, TWSIIRQS);
  218. spin_unlock_irqrestore(&cam->mcam.dev_lock, flags);
  219. }
  220. static u32 cafe_smbus_func(struct i2c_adapter *adapter)
  221. {
  222. return I2C_FUNC_SMBUS_READ_BYTE_DATA |
  223. I2C_FUNC_SMBUS_WRITE_BYTE_DATA;
  224. }
  225. static struct i2c_algorithm cafe_smbus_algo = {
  226. .smbus_xfer = cafe_smbus_xfer,
  227. .functionality = cafe_smbus_func
  228. };
  229. static int cafe_smbus_setup(struct cafe_camera *cam)
  230. {
  231. struct i2c_adapter *adap = &cam->mcam.i2c_adapter;
  232. int ret;
  233. cafe_smbus_enable_irq(cam);
  234. adap->owner = THIS_MODULE;
  235. adap->algo = &cafe_smbus_algo;
  236. strcpy(adap->name, "cafe_ccic");
  237. adap->dev.parent = &cam->pdev->dev;
  238. i2c_set_adapdata(adap, cam);
  239. ret = i2c_add_adapter(adap);
  240. if (ret)
  241. printk(KERN_ERR "Unable to register cafe i2c adapter\n");
  242. return ret;
  243. }
  244. static void cafe_smbus_shutdown(struct cafe_camera *cam)
  245. {
  246. i2c_del_adapter(&cam->mcam.i2c_adapter);
  247. }
  248. /*
  249. * Controller-level stuff
  250. */
  251. static void cafe_ctlr_init(struct mcam_camera *mcam)
  252. {
  253. unsigned long flags;
  254. spin_lock_irqsave(&mcam->dev_lock, flags);
  255. /*
  256. * Added magic to bring up the hardware on the B-Test board
  257. */
  258. mcam_reg_write(mcam, 0x3038, 0x8);
  259. mcam_reg_write(mcam, 0x315c, 0x80008);
  260. /*
  261. * Go through the dance needed to wake the device up.
  262. * Note that these registers are global and shared
  263. * with the NAND and SD devices. Interaction between the
  264. * three still needs to be examined.
  265. */
  266. mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRS|GCSR_MRS); /* Needed? */
  267. mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRC|GCSR_MRC);
  268. mcam_reg_write(mcam, REG_GL_CSR, GCSR_SRC|GCSR_MRS);
  269. /*
  270. * Here we must wait a bit for the controller to come around.
  271. */
  272. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  273. msleep(5);
  274. spin_lock_irqsave(&mcam->dev_lock, flags);
  275. mcam_reg_write(mcam, REG_GL_CSR, GCSR_CCIC_EN|GCSR_SRC|GCSR_MRC);
  276. mcam_reg_set_bit(mcam, REG_GL_IMASK, GIMSK_CCIC_EN);
  277. /*
  278. * Mask all interrupts.
  279. */
  280. mcam_reg_write(mcam, REG_IRQMASK, 0);
  281. spin_unlock_irqrestore(&mcam->dev_lock, flags);
  282. }
  283. static void cafe_ctlr_power_up(struct mcam_camera *mcam)
  284. {
  285. /*
  286. * Part one of the sensor dance: turn the global
  287. * GPIO signal on.
  288. */
  289. mcam_reg_write(mcam, REG_GL_FCR, GFCR_GPIO_ON);
  290. mcam_reg_write(mcam, REG_GL_GPIOR, GGPIO_OUT|GGPIO_VAL);
  291. /*
  292. * Put the sensor into operational mode (assumes OLPC-style
  293. * wiring). Control 0 is reset - set to 1 to operate.
  294. * Control 1 is power down, set to 0 to operate.
  295. */
  296. mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN); /* pwr up, reset */
  297. mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C0);
  298. }
  299. static void cafe_ctlr_power_down(struct mcam_camera *mcam)
  300. {
  301. mcam_reg_write(mcam, REG_GPR, GPR_C1EN|GPR_C0EN|GPR_C1);
  302. mcam_reg_write(mcam, REG_GL_FCR, GFCR_GPIO_ON);
  303. mcam_reg_write(mcam, REG_GL_GPIOR, GGPIO_OUT);
  304. }
  305. /*
  306. * The platform interrupt handler.
  307. */
  308. static irqreturn_t cafe_irq(int irq, void *data)
  309. {
  310. struct cafe_camera *cam = data;
  311. struct mcam_camera *mcam = &cam->mcam;
  312. unsigned int irqs, handled;
  313. spin_lock(&mcam->dev_lock);
  314. irqs = mcam_reg_read(mcam, REG_IRQSTAT);
  315. handled = cam->registered && mccic_irq(mcam, irqs);
  316. if (irqs & TWSIIRQS) {
  317. mcam_reg_write(mcam, REG_IRQSTAT, TWSIIRQS);
  318. wake_up(&cam->smbus_wait);
  319. handled = 1;
  320. }
  321. spin_unlock(&mcam->dev_lock);
  322. return IRQ_RETVAL(handled);
  323. }
  324. /* -------------------------------------------------------------------------- */
  325. /*
  326. * PCI interface stuff.
  327. */
  328. static int cafe_pci_probe(struct pci_dev *pdev,
  329. const struct pci_device_id *id)
  330. {
  331. int ret;
  332. struct cafe_camera *cam;
  333. struct mcam_camera *mcam;
  334. /*
  335. * Start putting together one of our big camera structures.
  336. */
  337. ret = -ENOMEM;
  338. cam = kzalloc(sizeof(struct cafe_camera), GFP_KERNEL);
  339. if (cam == NULL)
  340. goto out;
  341. cam->pdev = pdev;
  342. mcam = &cam->mcam;
  343. mcam->chip_id = V4L2_IDENT_CAFE;
  344. spin_lock_init(&mcam->dev_lock);
  345. init_waitqueue_head(&cam->smbus_wait);
  346. mcam->plat_power_up = cafe_ctlr_power_up;
  347. mcam->plat_power_down = cafe_ctlr_power_down;
  348. mcam->dev = &pdev->dev;
  349. /*
  350. * Set the clock speed for the XO 1; I don't believe this
  351. * driver has ever run anywhere else.
  352. */
  353. mcam->clock_speed = 45;
  354. mcam->use_smbus = 1;
  355. /*
  356. * Get set up on the PCI bus.
  357. */
  358. ret = pci_enable_device(pdev);
  359. if (ret)
  360. goto out_free;
  361. pci_set_master(pdev);
  362. ret = -EIO;
  363. mcam->regs = pci_iomap(pdev, 0, 0);
  364. if (!mcam->regs) {
  365. printk(KERN_ERR "Unable to ioremap cafe-ccic regs\n");
  366. goto out_disable;
  367. }
  368. ret = request_irq(pdev->irq, cafe_irq, IRQF_SHARED, "cafe-ccic", cam);
  369. if (ret)
  370. goto out_iounmap;
  371. /*
  372. * Initialize the controller and leave it powered up. It will
  373. * stay that way until the sensor driver shows up.
  374. */
  375. cafe_ctlr_init(mcam);
  376. cafe_ctlr_power_up(mcam);
  377. /*
  378. * Set up I2C/SMBUS communications. We have to drop the mutex here
  379. * because the sensor could attach in this call chain, leading to
  380. * unsightly deadlocks.
  381. */
  382. ret = cafe_smbus_setup(cam);
  383. if (ret)
  384. goto out_pdown;
  385. ret = mccic_register(mcam);
  386. if (ret == 0) {
  387. cam->registered = 1;
  388. return 0;
  389. }
  390. cafe_smbus_shutdown(cam);
  391. out_pdown:
  392. cafe_ctlr_power_down(mcam);
  393. free_irq(pdev->irq, cam);
  394. out_iounmap:
  395. pci_iounmap(pdev, mcam->regs);
  396. out_disable:
  397. pci_disable_device(pdev);
  398. out_free:
  399. kfree(cam);
  400. out:
  401. return ret;
  402. }
  403. /*
  404. * Shut down an initialized device
  405. */
  406. static void cafe_shutdown(struct cafe_camera *cam)
  407. {
  408. mccic_shutdown(&cam->mcam);
  409. cafe_smbus_shutdown(cam);
  410. free_irq(cam->pdev->irq, cam);
  411. pci_iounmap(cam->pdev, cam->mcam.regs);
  412. }
  413. static void cafe_pci_remove(struct pci_dev *pdev)
  414. {
  415. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  416. struct cafe_camera *cam = to_cam(v4l2_dev);
  417. if (cam == NULL) {
  418. printk(KERN_WARNING "pci_remove on unknown pdev %p\n", pdev);
  419. return;
  420. }
  421. cafe_shutdown(cam);
  422. kfree(cam);
  423. }
  424. #ifdef CONFIG_PM
  425. /*
  426. * Basic power management.
  427. */
  428. static int cafe_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  429. {
  430. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  431. struct cafe_camera *cam = to_cam(v4l2_dev);
  432. int ret;
  433. ret = pci_save_state(pdev);
  434. if (ret)
  435. return ret;
  436. mccic_suspend(&cam->mcam);
  437. pci_disable_device(pdev);
  438. return 0;
  439. }
  440. static int cafe_pci_resume(struct pci_dev *pdev)
  441. {
  442. struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
  443. struct cafe_camera *cam = to_cam(v4l2_dev);
  444. int ret = 0;
  445. pci_restore_state(pdev);
  446. ret = pci_enable_device(pdev);
  447. if (ret) {
  448. cam_warn(cam, "Unable to re-enable device on resume!\n");
  449. return ret;
  450. }
  451. cafe_ctlr_init(&cam->mcam);
  452. return mccic_resume(&cam->mcam);
  453. }
  454. #endif /* CONFIG_PM */
  455. static struct pci_device_id cafe_ids[] = {
  456. { PCI_DEVICE(PCI_VENDOR_ID_MARVELL,
  457. PCI_DEVICE_ID_MARVELL_88ALP01_CCIC) },
  458. { 0, }
  459. };
  460. MODULE_DEVICE_TABLE(pci, cafe_ids);
  461. static struct pci_driver cafe_pci_driver = {
  462. .name = "cafe1000-ccic",
  463. .id_table = cafe_ids,
  464. .probe = cafe_pci_probe,
  465. .remove = cafe_pci_remove,
  466. #ifdef CONFIG_PM
  467. .suspend = cafe_pci_suspend,
  468. .resume = cafe_pci_resume,
  469. #endif
  470. };
  471. static int __init cafe_init(void)
  472. {
  473. int ret;
  474. printk(KERN_NOTICE "Marvell M88ALP01 'CAFE' Camera Controller version %d\n",
  475. CAFE_VERSION);
  476. ret = pci_register_driver(&cafe_pci_driver);
  477. if (ret) {
  478. printk(KERN_ERR "Unable to register cafe_ccic driver\n");
  479. goto out;
  480. }
  481. ret = 0;
  482. out:
  483. return ret;
  484. }
  485. static void __exit cafe_exit(void)
  486. {
  487. pci_unregister_driver(&cafe_pci_driver);
  488. }
  489. module_init(cafe_init);
  490. module_exit(cafe_exit);