cafe-driver.c 17 KB

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