cafe-driver.c 14 KB

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