cafe-driver.c 18 KB

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