mcam-core.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  1. /*
  2. * The Marvell camera core. This device appears in a number of settings,
  3. * so it needs platform-specific support outside of the core.
  4. *
  5. * Copyright 2011 Jonathan Corbet corbet@lwn.net
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/i2c.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/slab.h>
  15. #include <linux/device.h>
  16. #include <linux/wait.h>
  17. #include <linux/list.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/delay.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/io.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-chip-ident.h>
  26. #include <media/ov7670.h>
  27. #include <media/videobuf2-vmalloc.h>
  28. #include <media/videobuf2-dma-contig.h>
  29. #include <media/videobuf2-dma-sg.h>
  30. #include "mcam-core.h"
  31. /*
  32. * Basic frame stats - to be deleted shortly
  33. */
  34. static int frames;
  35. static int singles;
  36. static int delivered;
  37. #ifdef MCAM_MODE_VMALLOC
  38. /*
  39. * Internal DMA buffer management. Since the controller cannot do S/G I/O,
  40. * we must have physically contiguous buffers to bring frames into.
  41. * These parameters control how many buffers we use, whether we
  42. * allocate them at load time (better chance of success, but nails down
  43. * memory) or when somebody tries to use the camera (riskier), and,
  44. * for load-time allocation, how big they should be.
  45. *
  46. * The controller can cycle through three buffers. We could use
  47. * more by flipping pointers around, but it probably makes little
  48. * sense.
  49. */
  50. static int alloc_bufs_at_read;
  51. module_param(alloc_bufs_at_read, bool, 0444);
  52. MODULE_PARM_DESC(alloc_bufs_at_read,
  53. "Non-zero value causes DMA buffers to be allocated when the "
  54. "video capture device is read, rather than at module load "
  55. "time. This saves memory, but decreases the chances of "
  56. "successfully getting those buffers. This parameter is "
  57. "only used in the vmalloc buffer mode");
  58. static int n_dma_bufs = 3;
  59. module_param(n_dma_bufs, uint, 0644);
  60. MODULE_PARM_DESC(n_dma_bufs,
  61. "The number of DMA buffers to allocate. Can be either two "
  62. "(saves memory, makes timing tighter) or three.");
  63. static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
  64. module_param(dma_buf_size, uint, 0444);
  65. MODULE_PARM_DESC(dma_buf_size,
  66. "The size of the allocated DMA buffers. If actual operating "
  67. "parameters require larger buffers, an attempt to reallocate "
  68. "will be made.");
  69. #else /* MCAM_MODE_VMALLOC */
  70. static const int alloc_bufs_at_read = 0;
  71. static const int n_dma_bufs = 3; /* Used by S/G_PARM */
  72. #endif /* MCAM_MODE_VMALLOC */
  73. static int flip;
  74. module_param(flip, bool, 0444);
  75. MODULE_PARM_DESC(flip,
  76. "If set, the sensor will be instructed to flip the image "
  77. "vertically.");
  78. static int buffer_mode = -1;
  79. module_param(buffer_mode, int, 0444);
  80. MODULE_PARM_DESC(buffer_mode,
  81. "Set the buffer mode to be used; default is to go with what "
  82. "the platform driver asks for. Set to 0 for vmalloc, 1 for "
  83. "DMA contiguous.");
  84. /*
  85. * Status flags. Always manipulated with bit operations.
  86. */
  87. #define CF_BUF0_VALID 0 /* Buffers valid - first three */
  88. #define CF_BUF1_VALID 1
  89. #define CF_BUF2_VALID 2
  90. #define CF_DMA_ACTIVE 3 /* A frame is incoming */
  91. #define CF_CONFIG_NEEDED 4 /* Must configure hardware */
  92. #define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
  93. #define CF_SG_RESTART 6 /* SG restart needed */
  94. #define sensor_call(cam, o, f, args...) \
  95. v4l2_subdev_call(cam->sensor, o, f, ##args)
  96. static struct mcam_format_struct {
  97. __u8 *desc;
  98. __u32 pixelformat;
  99. int bpp; /* Bytes per pixel */
  100. enum v4l2_mbus_pixelcode mbus_code;
  101. } mcam_formats[] = {
  102. {
  103. .desc = "YUYV 4:2:2",
  104. .pixelformat = V4L2_PIX_FMT_YUYV,
  105. .mbus_code = V4L2_MBUS_FMT_YUYV8_2X8,
  106. .bpp = 2,
  107. },
  108. {
  109. .desc = "RGB 444",
  110. .pixelformat = V4L2_PIX_FMT_RGB444,
  111. .mbus_code = V4L2_MBUS_FMT_RGB444_2X8_PADHI_LE,
  112. .bpp = 2,
  113. },
  114. {
  115. .desc = "RGB 565",
  116. .pixelformat = V4L2_PIX_FMT_RGB565,
  117. .mbus_code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  118. .bpp = 2,
  119. },
  120. {
  121. .desc = "Raw RGB Bayer",
  122. .pixelformat = V4L2_PIX_FMT_SBGGR8,
  123. .mbus_code = V4L2_MBUS_FMT_SBGGR8_1X8,
  124. .bpp = 1
  125. },
  126. };
  127. #define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
  128. static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
  129. {
  130. unsigned i;
  131. for (i = 0; i < N_MCAM_FMTS; i++)
  132. if (mcam_formats[i].pixelformat == pixelformat)
  133. return mcam_formats + i;
  134. /* Not found? Then return the first format. */
  135. return mcam_formats;
  136. }
  137. /*
  138. * The default format we use until somebody says otherwise.
  139. */
  140. static const struct v4l2_pix_format mcam_def_pix_format = {
  141. .width = VGA_WIDTH,
  142. .height = VGA_HEIGHT,
  143. .pixelformat = V4L2_PIX_FMT_YUYV,
  144. .field = V4L2_FIELD_NONE,
  145. .bytesperline = VGA_WIDTH*2,
  146. .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
  147. };
  148. static const enum v4l2_mbus_pixelcode mcam_def_mbus_code =
  149. V4L2_MBUS_FMT_YUYV8_2X8;
  150. /*
  151. * The two-word DMA descriptor format used by the Armada 610 and like. There
  152. * Is a three-word format as well (set C1_DESC_3WORD) where the third
  153. * word is a pointer to the next descriptor, but we don't use it. Two-word
  154. * descriptors have to be contiguous in memory.
  155. */
  156. struct mcam_dma_desc {
  157. u32 dma_addr;
  158. u32 segment_len;
  159. };
  160. /*
  161. * Our buffer type for working with videobuf2. Note that the vb2
  162. * developers have decreed that struct vb2_buffer must be at the
  163. * beginning of this structure.
  164. */
  165. struct mcam_vb_buffer {
  166. struct vb2_buffer vb_buf;
  167. struct list_head queue;
  168. struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
  169. dma_addr_t dma_desc_pa; /* Descriptor physical address */
  170. int dma_desc_nent; /* Number of mapped descriptors */
  171. };
  172. static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_buffer *vb)
  173. {
  174. return container_of(vb, struct mcam_vb_buffer, vb_buf);
  175. }
  176. /*
  177. * Hand a completed buffer back to user space.
  178. */
  179. static void mcam_buffer_done(struct mcam_camera *cam, int frame,
  180. struct vb2_buffer *vbuf)
  181. {
  182. vbuf->v4l2_buf.bytesused = cam->pix_format.sizeimage;
  183. vbuf->v4l2_buf.sequence = cam->buf_seq[frame];
  184. vb2_set_plane_payload(vbuf, 0, cam->pix_format.sizeimage);
  185. vb2_buffer_done(vbuf, VB2_BUF_STATE_DONE);
  186. }
  187. /*
  188. * Debugging and related.
  189. */
  190. #define cam_err(cam, fmt, arg...) \
  191. dev_err((cam)->dev, fmt, ##arg);
  192. #define cam_warn(cam, fmt, arg...) \
  193. dev_warn((cam)->dev, fmt, ##arg);
  194. #define cam_dbg(cam, fmt, arg...) \
  195. dev_dbg((cam)->dev, fmt, ##arg);
  196. /*
  197. * Flag manipulation helpers
  198. */
  199. static void mcam_reset_buffers(struct mcam_camera *cam)
  200. {
  201. int i;
  202. cam->next_buf = -1;
  203. for (i = 0; i < cam->nbufs; i++)
  204. clear_bit(i, &cam->flags);
  205. }
  206. static inline int mcam_needs_config(struct mcam_camera *cam)
  207. {
  208. return test_bit(CF_CONFIG_NEEDED, &cam->flags);
  209. }
  210. static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
  211. {
  212. if (needed)
  213. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  214. else
  215. clear_bit(CF_CONFIG_NEEDED, &cam->flags);
  216. }
  217. /* ------------------------------------------------------------------- */
  218. /*
  219. * Make the controller start grabbing images. Everything must
  220. * be set up before doing this.
  221. */
  222. static void mcam_ctlr_start(struct mcam_camera *cam)
  223. {
  224. /* set_bit performs a read, so no other barrier should be
  225. needed here */
  226. mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
  227. }
  228. static void mcam_ctlr_stop(struct mcam_camera *cam)
  229. {
  230. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  231. }
  232. /* ------------------------------------------------------------------- */
  233. #ifdef MCAM_MODE_VMALLOC
  234. /*
  235. * Code specific to the vmalloc buffer mode.
  236. */
  237. /*
  238. * Allocate in-kernel DMA buffers for vmalloc mode.
  239. */
  240. static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
  241. {
  242. int i;
  243. mcam_set_config_needed(cam, 1);
  244. if (loadtime)
  245. cam->dma_buf_size = dma_buf_size;
  246. else
  247. cam->dma_buf_size = cam->pix_format.sizeimage;
  248. if (n_dma_bufs > 3)
  249. n_dma_bufs = 3;
  250. cam->nbufs = 0;
  251. for (i = 0; i < n_dma_bufs; i++) {
  252. cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
  253. cam->dma_buf_size, cam->dma_handles + i,
  254. GFP_KERNEL);
  255. if (cam->dma_bufs[i] == NULL) {
  256. cam_warn(cam, "Failed to allocate DMA buffer\n");
  257. break;
  258. }
  259. (cam->nbufs)++;
  260. }
  261. switch (cam->nbufs) {
  262. case 1:
  263. dma_free_coherent(cam->dev, cam->dma_buf_size,
  264. cam->dma_bufs[0], cam->dma_handles[0]);
  265. cam->nbufs = 0;
  266. case 0:
  267. cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
  268. return -ENOMEM;
  269. case 2:
  270. if (n_dma_bufs > 2)
  271. cam_warn(cam, "Will limp along with only 2 buffers\n");
  272. break;
  273. }
  274. return 0;
  275. }
  276. static void mcam_free_dma_bufs(struct mcam_camera *cam)
  277. {
  278. int i;
  279. for (i = 0; i < cam->nbufs; i++) {
  280. dma_free_coherent(cam->dev, cam->dma_buf_size,
  281. cam->dma_bufs[i], cam->dma_handles[i]);
  282. cam->dma_bufs[i] = NULL;
  283. }
  284. cam->nbufs = 0;
  285. }
  286. /*
  287. * Set up DMA buffers when operating in vmalloc mode
  288. */
  289. static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
  290. {
  291. /*
  292. * Store the first two Y buffers (we aren't supporting
  293. * planar formats for now, so no UV bufs). Then either
  294. * set the third if it exists, or tell the controller
  295. * to just use two.
  296. */
  297. mcam_reg_write(cam, REG_Y0BAR, cam->dma_handles[0]);
  298. mcam_reg_write(cam, REG_Y1BAR, cam->dma_handles[1]);
  299. if (cam->nbufs > 2) {
  300. mcam_reg_write(cam, REG_Y2BAR, cam->dma_handles[2]);
  301. mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
  302. } else
  303. mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  304. if (cam->chip_id == V4L2_IDENT_CAFE)
  305. mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
  306. }
  307. /*
  308. * Copy data out to user space in the vmalloc case
  309. */
  310. static void mcam_frame_tasklet(unsigned long data)
  311. {
  312. struct mcam_camera *cam = (struct mcam_camera *) data;
  313. int i;
  314. unsigned long flags;
  315. struct mcam_vb_buffer *buf;
  316. spin_lock_irqsave(&cam->dev_lock, flags);
  317. for (i = 0; i < cam->nbufs; i++) {
  318. int bufno = cam->next_buf;
  319. if (cam->state != S_STREAMING || bufno < 0)
  320. break; /* I/O got stopped */
  321. if (++(cam->next_buf) >= cam->nbufs)
  322. cam->next_buf = 0;
  323. if (!test_bit(bufno, &cam->flags))
  324. continue;
  325. if (list_empty(&cam->buffers)) {
  326. singles++;
  327. break; /* Leave it valid, hope for better later */
  328. }
  329. delivered++;
  330. clear_bit(bufno, &cam->flags);
  331. buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
  332. queue);
  333. list_del_init(&buf->queue);
  334. /*
  335. * Drop the lock during the big copy. This *should* be safe...
  336. */
  337. spin_unlock_irqrestore(&cam->dev_lock, flags);
  338. memcpy(vb2_plane_vaddr(&buf->vb_buf, 0), cam->dma_bufs[bufno],
  339. cam->pix_format.sizeimage);
  340. mcam_buffer_done(cam, bufno, &buf->vb_buf);
  341. spin_lock_irqsave(&cam->dev_lock, flags);
  342. }
  343. spin_unlock_irqrestore(&cam->dev_lock, flags);
  344. }
  345. /*
  346. * Make sure our allocated buffers are up to the task.
  347. */
  348. static int mcam_check_dma_buffers(struct mcam_camera *cam)
  349. {
  350. if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
  351. mcam_free_dma_bufs(cam);
  352. if (cam->nbufs == 0)
  353. return mcam_alloc_dma_bufs(cam, 0);
  354. return 0;
  355. }
  356. static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
  357. {
  358. tasklet_schedule(&cam->s_tasklet);
  359. }
  360. #else /* MCAM_MODE_VMALLOC */
  361. static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
  362. {
  363. return 0;
  364. }
  365. static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
  366. {
  367. return;
  368. }
  369. static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
  370. {
  371. return 0;
  372. }
  373. #endif /* MCAM_MODE_VMALLOC */
  374. #ifdef MCAM_MODE_DMA_CONTIG
  375. /* ---------------------------------------------------------------------- */
  376. /*
  377. * DMA-contiguous code.
  378. */
  379. /*
  380. * Set up a contiguous buffer for the given frame. Here also is where
  381. * the underrun strategy is set: if there is no buffer available, reuse
  382. * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
  383. * keep the interrupt handler from giving that buffer back to user
  384. * space. In this way, we always have a buffer to DMA to and don't
  385. * have to try to play games stopping and restarting the controller.
  386. */
  387. static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
  388. {
  389. struct mcam_vb_buffer *buf;
  390. /*
  391. * If there are no available buffers, go into single mode
  392. */
  393. if (list_empty(&cam->buffers)) {
  394. buf = cam->vb_bufs[frame ^ 0x1];
  395. cam->vb_bufs[frame] = buf;
  396. mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
  397. vb2_dma_contig_plane_dma_addr(&buf->vb_buf, 0));
  398. set_bit(CF_SINGLE_BUFFER, &cam->flags);
  399. singles++;
  400. return;
  401. }
  402. /*
  403. * OK, we have a buffer we can use.
  404. */
  405. buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
  406. list_del_init(&buf->queue);
  407. mcam_reg_write(cam, frame == 0 ? REG_Y0BAR : REG_Y1BAR,
  408. vb2_dma_contig_plane_dma_addr(&buf->vb_buf, 0));
  409. cam->vb_bufs[frame] = buf;
  410. clear_bit(CF_SINGLE_BUFFER, &cam->flags);
  411. }
  412. /*
  413. * Initial B_DMA_contig setup.
  414. */
  415. static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
  416. {
  417. mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
  418. cam->nbufs = 2;
  419. mcam_set_contig_buffer(cam, 0);
  420. mcam_set_contig_buffer(cam, 1);
  421. }
  422. /*
  423. * Frame completion handling.
  424. */
  425. static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
  426. {
  427. struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
  428. if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
  429. delivered++;
  430. mcam_buffer_done(cam, frame, &buf->vb_buf);
  431. }
  432. mcam_set_contig_buffer(cam, frame);
  433. }
  434. #endif /* MCAM_MODE_DMA_CONTIG */
  435. #ifdef MCAM_MODE_DMA_SG
  436. /* ---------------------------------------------------------------------- */
  437. /*
  438. * Scatter/gather-specific code.
  439. */
  440. /*
  441. * Set up the next buffer for S/G I/O; caller should be sure that
  442. * the controller is stopped and a buffer is available.
  443. */
  444. static void mcam_sg_next_buffer(struct mcam_camera *cam)
  445. {
  446. struct mcam_vb_buffer *buf;
  447. buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
  448. list_del_init(&buf->queue);
  449. mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
  450. mcam_reg_write(cam, REG_DESC_LEN_Y,
  451. buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
  452. mcam_reg_write(cam, REG_DESC_LEN_U, 0);
  453. mcam_reg_write(cam, REG_DESC_LEN_V, 0);
  454. cam->vb_bufs[0] = buf;
  455. }
  456. /*
  457. * Initial B_DMA_sg setup
  458. */
  459. static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
  460. {
  461. mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
  462. mcam_sg_next_buffer(cam);
  463. mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
  464. cam->nbufs = 3;
  465. }
  466. /*
  467. * Frame completion with S/G is trickier. We can't muck with
  468. * a descriptor chain on the fly, since the controller buffers it
  469. * internally. So we have to actually stop and restart; Marvell
  470. * says this is the way to do it.
  471. *
  472. * Of course, stopping is easier said than done; experience shows
  473. * that the controller can start a frame *after* C0_ENABLE has been
  474. * cleared. So when running in S/G mode, the controller is "stopped"
  475. * on receipt of the start-of-frame interrupt. That means we can
  476. * safely change the DMA descriptor array here and restart things
  477. * (assuming there's another buffer waiting to go).
  478. */
  479. static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
  480. {
  481. struct mcam_vb_buffer *buf = cam->vb_bufs[0];
  482. /*
  483. * Very Bad Not Good Things happen if you don't clear
  484. * C1_DESC_ENA before making any descriptor changes.
  485. */
  486. mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
  487. /*
  488. * If we have another buffer available, put it in and
  489. * restart the engine.
  490. */
  491. if (!list_empty(&cam->buffers)) {
  492. mcam_sg_next_buffer(cam);
  493. mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
  494. mcam_ctlr_start(cam);
  495. /*
  496. * Otherwise set CF_SG_RESTART and the controller will
  497. * be restarted once another buffer shows up.
  498. */
  499. } else {
  500. set_bit(CF_SG_RESTART, &cam->flags);
  501. singles++;
  502. }
  503. /*
  504. * Now we can give the completed frame back to user space.
  505. */
  506. delivered++;
  507. mcam_buffer_done(cam, frame, &buf->vb_buf);
  508. }
  509. /*
  510. * Scatter/gather mode requires stopping the controller between
  511. * frames so we can put in a new DMA descriptor array. If no new
  512. * buffer exists at frame completion, the controller is left stopped;
  513. * this function is charged with gettig things going again.
  514. */
  515. static void mcam_sg_restart(struct mcam_camera *cam)
  516. {
  517. mcam_ctlr_dma_sg(cam);
  518. mcam_ctlr_start(cam);
  519. clear_bit(CF_SG_RESTART, &cam->flags);
  520. }
  521. #else /* MCAM_MODE_DMA_SG */
  522. static inline void mcam_sg_restart(struct mcam_camera *cam)
  523. {
  524. return;
  525. }
  526. #endif /* MCAM_MODE_DMA_SG */
  527. /* ---------------------------------------------------------------------- */
  528. /*
  529. * Buffer-mode-independent controller code.
  530. */
  531. /*
  532. * Image format setup
  533. */
  534. static void mcam_ctlr_image(struct mcam_camera *cam)
  535. {
  536. int imgsz;
  537. struct v4l2_pix_format *fmt = &cam->pix_format;
  538. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  539. (fmt->bytesperline & IMGSZ_H_MASK);
  540. mcam_reg_write(cam, REG_IMGSIZE, imgsz);
  541. mcam_reg_write(cam, REG_IMGOFFSET, 0);
  542. /* YPITCH just drops the last two bits */
  543. mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  544. IMGP_YP_MASK);
  545. /*
  546. * Tell the controller about the image format we are using.
  547. */
  548. switch (cam->pix_format.pixelformat) {
  549. case V4L2_PIX_FMT_YUYV:
  550. mcam_reg_write_mask(cam, REG_CTRL0,
  551. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  552. C0_DF_MASK);
  553. break;
  554. case V4L2_PIX_FMT_RGB444:
  555. mcam_reg_write_mask(cam, REG_CTRL0,
  556. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  557. C0_DF_MASK);
  558. /* Alpha value? */
  559. break;
  560. case V4L2_PIX_FMT_RGB565:
  561. mcam_reg_write_mask(cam, REG_CTRL0,
  562. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  563. C0_DF_MASK);
  564. break;
  565. default:
  566. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  567. break;
  568. }
  569. /*
  570. * Make sure it knows we want to use hsync/vsync.
  571. */
  572. mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  573. C0_SIFM_MASK);
  574. }
  575. /*
  576. * Configure the controller for operation; caller holds the
  577. * device mutex.
  578. */
  579. static int mcam_ctlr_configure(struct mcam_camera *cam)
  580. {
  581. unsigned long flags;
  582. spin_lock_irqsave(&cam->dev_lock, flags);
  583. cam->dma_setup(cam);
  584. mcam_ctlr_image(cam);
  585. mcam_set_config_needed(cam, 0);
  586. clear_bit(CF_SG_RESTART, &cam->flags);
  587. spin_unlock_irqrestore(&cam->dev_lock, flags);
  588. return 0;
  589. }
  590. static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
  591. {
  592. /*
  593. * Clear any pending interrupts, since we do not
  594. * expect to have I/O active prior to enabling.
  595. */
  596. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  597. mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  598. }
  599. static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
  600. {
  601. mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  602. }
  603. static void mcam_ctlr_init(struct mcam_camera *cam)
  604. {
  605. unsigned long flags;
  606. spin_lock_irqsave(&cam->dev_lock, flags);
  607. /*
  608. * Make sure it's not powered down.
  609. */
  610. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  611. /*
  612. * Turn off the enable bit. It sure should be off anyway,
  613. * but it's good to be sure.
  614. */
  615. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  616. /*
  617. * Clock the sensor appropriately. Controller clock should
  618. * be 48MHz, sensor "typical" value is half that.
  619. */
  620. mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  621. spin_unlock_irqrestore(&cam->dev_lock, flags);
  622. }
  623. /*
  624. * Stop the controller, and don't return until we're really sure that no
  625. * further DMA is going on.
  626. */
  627. static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
  628. {
  629. unsigned long flags;
  630. /*
  631. * Theory: stop the camera controller (whether it is operating
  632. * or not). Delay briefly just in case we race with the SOF
  633. * interrupt, then wait until no DMA is active.
  634. */
  635. spin_lock_irqsave(&cam->dev_lock, flags);
  636. clear_bit(CF_SG_RESTART, &cam->flags);
  637. mcam_ctlr_stop(cam);
  638. cam->state = S_IDLE;
  639. spin_unlock_irqrestore(&cam->dev_lock, flags);
  640. msleep(40);
  641. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  642. cam_err(cam, "Timeout waiting for DMA to end\n");
  643. /* This would be bad news - what now? */
  644. spin_lock_irqsave(&cam->dev_lock, flags);
  645. mcam_ctlr_irq_disable(cam);
  646. spin_unlock_irqrestore(&cam->dev_lock, flags);
  647. }
  648. /*
  649. * Power up and down.
  650. */
  651. static void mcam_ctlr_power_up(struct mcam_camera *cam)
  652. {
  653. unsigned long flags;
  654. spin_lock_irqsave(&cam->dev_lock, flags);
  655. cam->plat_power_up(cam);
  656. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  657. spin_unlock_irqrestore(&cam->dev_lock, flags);
  658. msleep(5); /* Just to be sure */
  659. }
  660. static void mcam_ctlr_power_down(struct mcam_camera *cam)
  661. {
  662. unsigned long flags;
  663. spin_lock_irqsave(&cam->dev_lock, flags);
  664. /*
  665. * School of hard knocks department: be sure we do any register
  666. * twiddling on the controller *before* calling the platform
  667. * power down routine.
  668. */
  669. mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  670. cam->plat_power_down(cam);
  671. spin_unlock_irqrestore(&cam->dev_lock, flags);
  672. }
  673. /* -------------------------------------------------------------------- */
  674. /*
  675. * Communications with the sensor.
  676. */
  677. static int __mcam_cam_reset(struct mcam_camera *cam)
  678. {
  679. return sensor_call(cam, core, reset, 0);
  680. }
  681. /*
  682. * We have found the sensor on the i2c. Let's try to have a
  683. * conversation.
  684. */
  685. static int mcam_cam_init(struct mcam_camera *cam)
  686. {
  687. struct v4l2_dbg_chip_ident chip;
  688. int ret;
  689. mutex_lock(&cam->s_mutex);
  690. if (cam->state != S_NOTREADY)
  691. cam_warn(cam, "Cam init with device in funky state %d",
  692. cam->state);
  693. ret = __mcam_cam_reset(cam);
  694. if (ret)
  695. goto out;
  696. chip.ident = V4L2_IDENT_NONE;
  697. chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
  698. chip.match.addr = cam->sensor_addr;
  699. ret = sensor_call(cam, core, g_chip_ident, &chip);
  700. if (ret)
  701. goto out;
  702. cam->sensor_type = chip.ident;
  703. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  704. cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
  705. ret = -EINVAL;
  706. goto out;
  707. }
  708. /* Get/set parameters? */
  709. ret = 0;
  710. cam->state = S_IDLE;
  711. out:
  712. mcam_ctlr_power_down(cam);
  713. mutex_unlock(&cam->s_mutex);
  714. return ret;
  715. }
  716. /*
  717. * Configure the sensor to match the parameters we have. Caller should
  718. * hold s_mutex
  719. */
  720. static int mcam_cam_set_flip(struct mcam_camera *cam)
  721. {
  722. struct v4l2_control ctrl;
  723. memset(&ctrl, 0, sizeof(ctrl));
  724. ctrl.id = V4L2_CID_VFLIP;
  725. ctrl.value = flip;
  726. return sensor_call(cam, core, s_ctrl, &ctrl);
  727. }
  728. static int mcam_cam_configure(struct mcam_camera *cam)
  729. {
  730. struct v4l2_mbus_framefmt mbus_fmt;
  731. int ret;
  732. v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
  733. ret = sensor_call(cam, core, init, 0);
  734. if (ret == 0)
  735. ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
  736. /*
  737. * OV7670 does weird things if flip is set *before* format...
  738. */
  739. ret += mcam_cam_set_flip(cam);
  740. return ret;
  741. }
  742. /*
  743. * Get everything ready, and start grabbing frames.
  744. */
  745. static int mcam_read_setup(struct mcam_camera *cam)
  746. {
  747. int ret;
  748. unsigned long flags;
  749. /*
  750. * Configuration. If we still don't have DMA buffers,
  751. * make one last, desperate attempt.
  752. */
  753. if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
  754. mcam_alloc_dma_bufs(cam, 0))
  755. return -ENOMEM;
  756. if (mcam_needs_config(cam)) {
  757. mcam_cam_configure(cam);
  758. ret = mcam_ctlr_configure(cam);
  759. if (ret)
  760. return ret;
  761. }
  762. /*
  763. * Turn it loose.
  764. */
  765. spin_lock_irqsave(&cam->dev_lock, flags);
  766. mcam_reset_buffers(cam);
  767. mcam_ctlr_irq_enable(cam);
  768. cam->state = S_STREAMING;
  769. mcam_ctlr_start(cam);
  770. spin_unlock_irqrestore(&cam->dev_lock, flags);
  771. return 0;
  772. }
  773. /* ----------------------------------------------------------------------- */
  774. /*
  775. * Videobuf2 interface code.
  776. */
  777. static int mcam_vb_queue_setup(struct vb2_queue *vq,
  778. const struct v4l2_format *fmt, unsigned int *nbufs,
  779. unsigned int *num_planes, unsigned int sizes[],
  780. void *alloc_ctxs[])
  781. {
  782. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  783. int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
  784. sizes[0] = cam->pix_format.sizeimage;
  785. *num_planes = 1; /* Someday we have to support planar formats... */
  786. if (*nbufs < minbufs)
  787. *nbufs = minbufs;
  788. if (cam->buffer_mode == B_DMA_contig)
  789. alloc_ctxs[0] = cam->vb_alloc_ctx;
  790. return 0;
  791. }
  792. static void mcam_vb_buf_queue(struct vb2_buffer *vb)
  793. {
  794. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  795. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  796. unsigned long flags;
  797. int start;
  798. spin_lock_irqsave(&cam->dev_lock, flags);
  799. start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
  800. list_add(&mvb->queue, &cam->buffers);
  801. if (test_bit(CF_SG_RESTART, &cam->flags))
  802. mcam_sg_restart(cam);
  803. spin_unlock_irqrestore(&cam->dev_lock, flags);
  804. if (start)
  805. mcam_read_setup(cam);
  806. }
  807. /*
  808. * vb2 uses these to release the mutex when waiting in dqbuf. I'm
  809. * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
  810. * to be called with the mutex held), but better safe than sorry.
  811. */
  812. static void mcam_vb_wait_prepare(struct vb2_queue *vq)
  813. {
  814. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  815. mutex_unlock(&cam->s_mutex);
  816. }
  817. static void mcam_vb_wait_finish(struct vb2_queue *vq)
  818. {
  819. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  820. mutex_lock(&cam->s_mutex);
  821. }
  822. /*
  823. * These need to be called with the mutex held from vb2
  824. */
  825. static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
  826. {
  827. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  828. if (cam->state != S_IDLE) {
  829. INIT_LIST_HEAD(&cam->buffers);
  830. return -EINVAL;
  831. }
  832. cam->sequence = 0;
  833. /*
  834. * Videobuf2 sneakily hoards all the buffers and won't
  835. * give them to us until *after* streaming starts. But
  836. * we can't actually start streaming until we have a
  837. * destination. So go into a wait state and hope they
  838. * give us buffers soon.
  839. */
  840. if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
  841. cam->state = S_BUFWAIT;
  842. return 0;
  843. }
  844. return mcam_read_setup(cam);
  845. }
  846. static int mcam_vb_stop_streaming(struct vb2_queue *vq)
  847. {
  848. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  849. unsigned long flags;
  850. if (cam->state == S_BUFWAIT) {
  851. /* They never gave us buffers */
  852. cam->state = S_IDLE;
  853. return 0;
  854. }
  855. if (cam->state != S_STREAMING)
  856. return -EINVAL;
  857. mcam_ctlr_stop_dma(cam);
  858. /*
  859. * VB2 reclaims the buffers, so we need to forget
  860. * about them.
  861. */
  862. spin_lock_irqsave(&cam->dev_lock, flags);
  863. INIT_LIST_HEAD(&cam->buffers);
  864. spin_unlock_irqrestore(&cam->dev_lock, flags);
  865. return 0;
  866. }
  867. static const struct vb2_ops mcam_vb2_ops = {
  868. .queue_setup = mcam_vb_queue_setup,
  869. .buf_queue = mcam_vb_buf_queue,
  870. .start_streaming = mcam_vb_start_streaming,
  871. .stop_streaming = mcam_vb_stop_streaming,
  872. .wait_prepare = mcam_vb_wait_prepare,
  873. .wait_finish = mcam_vb_wait_finish,
  874. };
  875. #ifdef MCAM_MODE_DMA_SG
  876. /*
  877. * Scatter/gather mode uses all of the above functions plus a
  878. * few extras to deal with DMA mapping.
  879. */
  880. static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
  881. {
  882. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  883. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  884. int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
  885. mvb->dma_desc = dma_alloc_coherent(cam->dev,
  886. ndesc * sizeof(struct mcam_dma_desc),
  887. &mvb->dma_desc_pa, GFP_KERNEL);
  888. if (mvb->dma_desc == NULL) {
  889. cam_err(cam, "Unable to get DMA descriptor array\n");
  890. return -ENOMEM;
  891. }
  892. return 0;
  893. }
  894. static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
  895. {
  896. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  897. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  898. struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
  899. struct mcam_dma_desc *desc = mvb->dma_desc;
  900. struct scatterlist *sg;
  901. int i;
  902. mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages,
  903. DMA_FROM_DEVICE);
  904. if (mvb->dma_desc_nent <= 0)
  905. return -EIO; /* Not sure what's right here */
  906. for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) {
  907. desc->dma_addr = sg_dma_address(sg);
  908. desc->segment_len = sg_dma_len(sg);
  909. desc++;
  910. }
  911. return 0;
  912. }
  913. static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb)
  914. {
  915. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  916. struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
  917. dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE);
  918. return 0;
  919. }
  920. static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
  921. {
  922. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  923. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  924. int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
  925. dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
  926. mvb->dma_desc, mvb->dma_desc_pa);
  927. }
  928. static const struct vb2_ops mcam_vb2_sg_ops = {
  929. .queue_setup = mcam_vb_queue_setup,
  930. .buf_init = mcam_vb_sg_buf_init,
  931. .buf_prepare = mcam_vb_sg_buf_prepare,
  932. .buf_queue = mcam_vb_buf_queue,
  933. .buf_finish = mcam_vb_sg_buf_finish,
  934. .buf_cleanup = mcam_vb_sg_buf_cleanup,
  935. .start_streaming = mcam_vb_start_streaming,
  936. .stop_streaming = mcam_vb_stop_streaming,
  937. .wait_prepare = mcam_vb_wait_prepare,
  938. .wait_finish = mcam_vb_wait_finish,
  939. };
  940. #endif /* MCAM_MODE_DMA_SG */
  941. static int mcam_setup_vb2(struct mcam_camera *cam)
  942. {
  943. struct vb2_queue *vq = &cam->vb_queue;
  944. memset(vq, 0, sizeof(*vq));
  945. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  946. vq->drv_priv = cam;
  947. INIT_LIST_HEAD(&cam->buffers);
  948. switch (cam->buffer_mode) {
  949. case B_DMA_contig:
  950. #ifdef MCAM_MODE_DMA_CONTIG
  951. vq->ops = &mcam_vb2_ops;
  952. vq->mem_ops = &vb2_dma_contig_memops;
  953. cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
  954. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  955. cam->dma_setup = mcam_ctlr_dma_contig;
  956. cam->frame_complete = mcam_dma_contig_done;
  957. #endif
  958. break;
  959. case B_DMA_sg:
  960. #ifdef MCAM_MODE_DMA_SG
  961. vq->ops = &mcam_vb2_sg_ops;
  962. vq->mem_ops = &vb2_dma_sg_memops;
  963. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  964. cam->dma_setup = mcam_ctlr_dma_sg;
  965. cam->frame_complete = mcam_dma_sg_done;
  966. #endif
  967. break;
  968. case B_vmalloc:
  969. #ifdef MCAM_MODE_VMALLOC
  970. tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
  971. (unsigned long) cam);
  972. vq->ops = &mcam_vb2_ops;
  973. vq->mem_ops = &vb2_vmalloc_memops;
  974. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  975. vq->io_modes = VB2_MMAP;
  976. cam->dma_setup = mcam_ctlr_dma_vmalloc;
  977. cam->frame_complete = mcam_vmalloc_done;
  978. #endif
  979. break;
  980. }
  981. return vb2_queue_init(vq);
  982. }
  983. static void mcam_cleanup_vb2(struct mcam_camera *cam)
  984. {
  985. vb2_queue_release(&cam->vb_queue);
  986. #ifdef MCAM_MODE_DMA_CONTIG
  987. if (cam->buffer_mode == B_DMA_contig)
  988. vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
  989. #endif
  990. }
  991. /* ---------------------------------------------------------------------- */
  992. /*
  993. * The long list of V4L2 ioctl() operations.
  994. */
  995. static int mcam_vidioc_streamon(struct file *filp, void *priv,
  996. enum v4l2_buf_type type)
  997. {
  998. struct mcam_camera *cam = filp->private_data;
  999. int ret;
  1000. mutex_lock(&cam->s_mutex);
  1001. ret = vb2_streamon(&cam->vb_queue, type);
  1002. mutex_unlock(&cam->s_mutex);
  1003. return ret;
  1004. }
  1005. static int mcam_vidioc_streamoff(struct file *filp, void *priv,
  1006. enum v4l2_buf_type type)
  1007. {
  1008. struct mcam_camera *cam = filp->private_data;
  1009. int ret;
  1010. mutex_lock(&cam->s_mutex);
  1011. ret = vb2_streamoff(&cam->vb_queue, type);
  1012. mutex_unlock(&cam->s_mutex);
  1013. return ret;
  1014. }
  1015. static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
  1016. struct v4l2_requestbuffers *req)
  1017. {
  1018. struct mcam_camera *cam = filp->private_data;
  1019. int ret;
  1020. mutex_lock(&cam->s_mutex);
  1021. ret = vb2_reqbufs(&cam->vb_queue, req);
  1022. mutex_unlock(&cam->s_mutex);
  1023. return ret;
  1024. }
  1025. static int mcam_vidioc_querybuf(struct file *filp, void *priv,
  1026. struct v4l2_buffer *buf)
  1027. {
  1028. struct mcam_camera *cam = filp->private_data;
  1029. int ret;
  1030. mutex_lock(&cam->s_mutex);
  1031. ret = vb2_querybuf(&cam->vb_queue, buf);
  1032. mutex_unlock(&cam->s_mutex);
  1033. return ret;
  1034. }
  1035. static int mcam_vidioc_qbuf(struct file *filp, void *priv,
  1036. struct v4l2_buffer *buf)
  1037. {
  1038. struct mcam_camera *cam = filp->private_data;
  1039. int ret;
  1040. mutex_lock(&cam->s_mutex);
  1041. ret = vb2_qbuf(&cam->vb_queue, buf);
  1042. mutex_unlock(&cam->s_mutex);
  1043. return ret;
  1044. }
  1045. static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
  1046. struct v4l2_buffer *buf)
  1047. {
  1048. struct mcam_camera *cam = filp->private_data;
  1049. int ret;
  1050. mutex_lock(&cam->s_mutex);
  1051. ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
  1052. mutex_unlock(&cam->s_mutex);
  1053. return ret;
  1054. }
  1055. static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
  1056. struct v4l2_queryctrl *qc)
  1057. {
  1058. struct mcam_camera *cam = priv;
  1059. int ret;
  1060. mutex_lock(&cam->s_mutex);
  1061. ret = sensor_call(cam, core, queryctrl, qc);
  1062. mutex_unlock(&cam->s_mutex);
  1063. return ret;
  1064. }
  1065. static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
  1066. struct v4l2_control *ctrl)
  1067. {
  1068. struct mcam_camera *cam = priv;
  1069. int ret;
  1070. mutex_lock(&cam->s_mutex);
  1071. ret = sensor_call(cam, core, g_ctrl, ctrl);
  1072. mutex_unlock(&cam->s_mutex);
  1073. return ret;
  1074. }
  1075. static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
  1076. struct v4l2_control *ctrl)
  1077. {
  1078. struct mcam_camera *cam = priv;
  1079. int ret;
  1080. mutex_lock(&cam->s_mutex);
  1081. ret = sensor_call(cam, core, s_ctrl, ctrl);
  1082. mutex_unlock(&cam->s_mutex);
  1083. return ret;
  1084. }
  1085. static int mcam_vidioc_querycap(struct file *file, void *priv,
  1086. struct v4l2_capability *cap)
  1087. {
  1088. strcpy(cap->driver, "marvell_ccic");
  1089. strcpy(cap->card, "marvell_ccic");
  1090. cap->version = 1;
  1091. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1092. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1093. return 0;
  1094. }
  1095. static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
  1096. void *priv, struct v4l2_fmtdesc *fmt)
  1097. {
  1098. if (fmt->index >= N_MCAM_FMTS)
  1099. return -EINVAL;
  1100. strlcpy(fmt->description, mcam_formats[fmt->index].desc,
  1101. sizeof(fmt->description));
  1102. fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
  1103. return 0;
  1104. }
  1105. static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1106. struct v4l2_format *fmt)
  1107. {
  1108. struct mcam_camera *cam = priv;
  1109. struct mcam_format_struct *f;
  1110. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  1111. struct v4l2_mbus_framefmt mbus_fmt;
  1112. int ret;
  1113. f = mcam_find_format(pix->pixelformat);
  1114. pix->pixelformat = f->pixelformat;
  1115. v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
  1116. mutex_lock(&cam->s_mutex);
  1117. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  1118. mutex_unlock(&cam->s_mutex);
  1119. v4l2_fill_pix_format(pix, &mbus_fmt);
  1120. pix->bytesperline = pix->width * f->bpp;
  1121. pix->sizeimage = pix->height * pix->bytesperline;
  1122. return ret;
  1123. }
  1124. static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1125. struct v4l2_format *fmt)
  1126. {
  1127. struct mcam_camera *cam = priv;
  1128. struct mcam_format_struct *f;
  1129. int ret;
  1130. /*
  1131. * Can't do anything if the device is not idle
  1132. * Also can't if there are streaming buffers in place.
  1133. */
  1134. if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
  1135. return -EBUSY;
  1136. f = mcam_find_format(fmt->fmt.pix.pixelformat);
  1137. /*
  1138. * See if the formatting works in principle.
  1139. */
  1140. ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  1141. if (ret)
  1142. return ret;
  1143. /*
  1144. * Now we start to change things for real, so let's do it
  1145. * under lock.
  1146. */
  1147. mutex_lock(&cam->s_mutex);
  1148. cam->pix_format = fmt->fmt.pix;
  1149. cam->mbus_code = f->mbus_code;
  1150. /*
  1151. * Make sure we have appropriate DMA buffers.
  1152. */
  1153. if (cam->buffer_mode == B_vmalloc) {
  1154. ret = mcam_check_dma_buffers(cam);
  1155. if (ret)
  1156. goto out;
  1157. }
  1158. mcam_set_config_needed(cam, 1);
  1159. ret = 0;
  1160. out:
  1161. mutex_unlock(&cam->s_mutex);
  1162. return ret;
  1163. }
  1164. /*
  1165. * Return our stored notion of how the camera is/should be configured.
  1166. * The V4l2 spec wants us to be smarter, and actually get this from
  1167. * the camera (and not mess with it at open time). Someday.
  1168. */
  1169. static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1170. struct v4l2_format *f)
  1171. {
  1172. struct mcam_camera *cam = priv;
  1173. f->fmt.pix = cam->pix_format;
  1174. return 0;
  1175. }
  1176. /*
  1177. * We only have one input - the sensor - so minimize the nonsense here.
  1178. */
  1179. static int mcam_vidioc_enum_input(struct file *filp, void *priv,
  1180. struct v4l2_input *input)
  1181. {
  1182. if (input->index != 0)
  1183. return -EINVAL;
  1184. input->type = V4L2_INPUT_TYPE_CAMERA;
  1185. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1186. strcpy(input->name, "Camera");
  1187. return 0;
  1188. }
  1189. static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1190. {
  1191. *i = 0;
  1192. return 0;
  1193. }
  1194. static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1195. {
  1196. if (i != 0)
  1197. return -EINVAL;
  1198. return 0;
  1199. }
  1200. /* from vivi.c */
  1201. static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  1202. {
  1203. return 0;
  1204. }
  1205. /*
  1206. * G/S_PARM. Most of this is done by the sensor, but we are
  1207. * the level which controls the number of read buffers.
  1208. */
  1209. static int mcam_vidioc_g_parm(struct file *filp, void *priv,
  1210. struct v4l2_streamparm *parms)
  1211. {
  1212. struct mcam_camera *cam = priv;
  1213. int ret;
  1214. mutex_lock(&cam->s_mutex);
  1215. ret = sensor_call(cam, video, g_parm, parms);
  1216. mutex_unlock(&cam->s_mutex);
  1217. parms->parm.capture.readbuffers = n_dma_bufs;
  1218. return ret;
  1219. }
  1220. static int mcam_vidioc_s_parm(struct file *filp, void *priv,
  1221. struct v4l2_streamparm *parms)
  1222. {
  1223. struct mcam_camera *cam = priv;
  1224. int ret;
  1225. mutex_lock(&cam->s_mutex);
  1226. ret = sensor_call(cam, video, s_parm, parms);
  1227. mutex_unlock(&cam->s_mutex);
  1228. parms->parm.capture.readbuffers = n_dma_bufs;
  1229. return ret;
  1230. }
  1231. static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
  1232. struct v4l2_dbg_chip_ident *chip)
  1233. {
  1234. struct mcam_camera *cam = priv;
  1235. chip->ident = V4L2_IDENT_NONE;
  1236. chip->revision = 0;
  1237. if (v4l2_chip_match_host(&chip->match)) {
  1238. chip->ident = cam->chip_id;
  1239. return 0;
  1240. }
  1241. return sensor_call(cam, core, g_chip_ident, chip);
  1242. }
  1243. static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
  1244. struct v4l2_frmsizeenum *sizes)
  1245. {
  1246. struct mcam_camera *cam = priv;
  1247. int ret;
  1248. mutex_lock(&cam->s_mutex);
  1249. ret = sensor_call(cam, video, enum_framesizes, sizes);
  1250. mutex_unlock(&cam->s_mutex);
  1251. return ret;
  1252. }
  1253. static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
  1254. struct v4l2_frmivalenum *interval)
  1255. {
  1256. struct mcam_camera *cam = priv;
  1257. int ret;
  1258. mutex_lock(&cam->s_mutex);
  1259. ret = sensor_call(cam, video, enum_frameintervals, interval);
  1260. mutex_unlock(&cam->s_mutex);
  1261. return ret;
  1262. }
  1263. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1264. static int mcam_vidioc_g_register(struct file *file, void *priv,
  1265. struct v4l2_dbg_register *reg)
  1266. {
  1267. struct mcam_camera *cam = priv;
  1268. if (v4l2_chip_match_host(&reg->match)) {
  1269. reg->val = mcam_reg_read(cam, reg->reg);
  1270. reg->size = 4;
  1271. return 0;
  1272. }
  1273. return sensor_call(cam, core, g_register, reg);
  1274. }
  1275. static int mcam_vidioc_s_register(struct file *file, void *priv,
  1276. struct v4l2_dbg_register *reg)
  1277. {
  1278. struct mcam_camera *cam = priv;
  1279. if (v4l2_chip_match_host(&reg->match)) {
  1280. mcam_reg_write(cam, reg->reg, reg->val);
  1281. return 0;
  1282. }
  1283. return sensor_call(cam, core, s_register, reg);
  1284. }
  1285. #endif
  1286. static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
  1287. .vidioc_querycap = mcam_vidioc_querycap,
  1288. .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
  1289. .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
  1290. .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
  1291. .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
  1292. .vidioc_enum_input = mcam_vidioc_enum_input,
  1293. .vidioc_g_input = mcam_vidioc_g_input,
  1294. .vidioc_s_input = mcam_vidioc_s_input,
  1295. .vidioc_s_std = mcam_vidioc_s_std,
  1296. .vidioc_reqbufs = mcam_vidioc_reqbufs,
  1297. .vidioc_querybuf = mcam_vidioc_querybuf,
  1298. .vidioc_qbuf = mcam_vidioc_qbuf,
  1299. .vidioc_dqbuf = mcam_vidioc_dqbuf,
  1300. .vidioc_streamon = mcam_vidioc_streamon,
  1301. .vidioc_streamoff = mcam_vidioc_streamoff,
  1302. .vidioc_queryctrl = mcam_vidioc_queryctrl,
  1303. .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
  1304. .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
  1305. .vidioc_g_parm = mcam_vidioc_g_parm,
  1306. .vidioc_s_parm = mcam_vidioc_s_parm,
  1307. .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
  1308. .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
  1309. .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
  1310. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1311. .vidioc_g_register = mcam_vidioc_g_register,
  1312. .vidioc_s_register = mcam_vidioc_s_register,
  1313. #endif
  1314. };
  1315. /* ---------------------------------------------------------------------- */
  1316. /*
  1317. * Our various file operations.
  1318. */
  1319. static int mcam_v4l_open(struct file *filp)
  1320. {
  1321. struct mcam_camera *cam = video_drvdata(filp);
  1322. int ret = 0;
  1323. filp->private_data = cam;
  1324. frames = singles = delivered = 0;
  1325. mutex_lock(&cam->s_mutex);
  1326. if (cam->users == 0) {
  1327. ret = mcam_setup_vb2(cam);
  1328. if (ret)
  1329. goto out;
  1330. mcam_ctlr_power_up(cam);
  1331. __mcam_cam_reset(cam);
  1332. mcam_set_config_needed(cam, 1);
  1333. }
  1334. (cam->users)++;
  1335. out:
  1336. mutex_unlock(&cam->s_mutex);
  1337. return ret;
  1338. }
  1339. static int mcam_v4l_release(struct file *filp)
  1340. {
  1341. struct mcam_camera *cam = filp->private_data;
  1342. cam_err(cam, "Release, %d frames, %d singles, %d delivered\n", frames,
  1343. singles, delivered);
  1344. mutex_lock(&cam->s_mutex);
  1345. (cam->users)--;
  1346. if (filp == cam->owner) {
  1347. mcam_ctlr_stop_dma(cam);
  1348. cam->owner = NULL;
  1349. }
  1350. if (cam->users == 0) {
  1351. mcam_cleanup_vb2(cam);
  1352. mcam_ctlr_power_down(cam);
  1353. if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
  1354. mcam_free_dma_bufs(cam);
  1355. }
  1356. mutex_unlock(&cam->s_mutex);
  1357. return 0;
  1358. }
  1359. static ssize_t mcam_v4l_read(struct file *filp,
  1360. char __user *buffer, size_t len, loff_t *pos)
  1361. {
  1362. struct mcam_camera *cam = filp->private_data;
  1363. int ret;
  1364. mutex_lock(&cam->s_mutex);
  1365. ret = vb2_read(&cam->vb_queue, buffer, len, pos,
  1366. filp->f_flags & O_NONBLOCK);
  1367. mutex_unlock(&cam->s_mutex);
  1368. return ret;
  1369. }
  1370. static unsigned int mcam_v4l_poll(struct file *filp,
  1371. struct poll_table_struct *pt)
  1372. {
  1373. struct mcam_camera *cam = filp->private_data;
  1374. int ret;
  1375. mutex_lock(&cam->s_mutex);
  1376. ret = vb2_poll(&cam->vb_queue, filp, pt);
  1377. mutex_unlock(&cam->s_mutex);
  1378. return ret;
  1379. }
  1380. static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1381. {
  1382. struct mcam_camera *cam = filp->private_data;
  1383. int ret;
  1384. mutex_lock(&cam->s_mutex);
  1385. ret = vb2_mmap(&cam->vb_queue, vma);
  1386. mutex_unlock(&cam->s_mutex);
  1387. return ret;
  1388. }
  1389. static const struct v4l2_file_operations mcam_v4l_fops = {
  1390. .owner = THIS_MODULE,
  1391. .open = mcam_v4l_open,
  1392. .release = mcam_v4l_release,
  1393. .read = mcam_v4l_read,
  1394. .poll = mcam_v4l_poll,
  1395. .mmap = mcam_v4l_mmap,
  1396. .unlocked_ioctl = video_ioctl2,
  1397. };
  1398. /*
  1399. * This template device holds all of those v4l2 methods; we
  1400. * clone it for specific real devices.
  1401. */
  1402. static struct video_device mcam_v4l_template = {
  1403. .name = "mcam",
  1404. .tvnorms = V4L2_STD_NTSC_M,
  1405. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1406. .fops = &mcam_v4l_fops,
  1407. .ioctl_ops = &mcam_v4l_ioctl_ops,
  1408. .release = video_device_release_empty,
  1409. };
  1410. /* ---------------------------------------------------------------------- */
  1411. /*
  1412. * Interrupt handler stuff
  1413. */
  1414. static void mcam_frame_complete(struct mcam_camera *cam, int frame)
  1415. {
  1416. /*
  1417. * Basic frame housekeeping.
  1418. */
  1419. set_bit(frame, &cam->flags);
  1420. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1421. cam->next_buf = frame;
  1422. cam->buf_seq[frame] = ++(cam->sequence);
  1423. frames++;
  1424. /*
  1425. * "This should never happen"
  1426. */
  1427. if (cam->state != S_STREAMING)
  1428. return;
  1429. /*
  1430. * Process the frame and set up the next one.
  1431. */
  1432. cam->frame_complete(cam, frame);
  1433. }
  1434. /*
  1435. * The interrupt handler; this needs to be called from the
  1436. * platform irq handler with the lock held.
  1437. */
  1438. int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
  1439. {
  1440. unsigned int frame, handled = 0;
  1441. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1442. /*
  1443. * Handle any frame completions. There really should
  1444. * not be more than one of these, or we have fallen
  1445. * far behind.
  1446. *
  1447. * When running in S/G mode, the frame number lacks any
  1448. * real meaning - there's only one descriptor array - but
  1449. * the controller still picks a different one to signal
  1450. * each time.
  1451. */
  1452. for (frame = 0; frame < cam->nbufs; frame++)
  1453. if (irqs & (IRQ_EOF0 << frame)) {
  1454. mcam_frame_complete(cam, frame);
  1455. handled = 1;
  1456. }
  1457. /*
  1458. * If a frame starts, note that we have DMA active. This
  1459. * code assumes that we won't get multiple frame interrupts
  1460. * at once; may want to rethink that.
  1461. */
  1462. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
  1463. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1464. handled = 1;
  1465. if (cam->buffer_mode == B_DMA_sg)
  1466. mcam_ctlr_stop(cam);
  1467. }
  1468. return handled;
  1469. }
  1470. /* ---------------------------------------------------------------------- */
  1471. /*
  1472. * Registration and such.
  1473. */
  1474. static struct ov7670_config sensor_cfg = {
  1475. /*
  1476. * Exclude QCIF mode, because it only captures a tiny portion
  1477. * of the sensor FOV
  1478. */
  1479. .min_width = 320,
  1480. .min_height = 240,
  1481. };
  1482. int mccic_register(struct mcam_camera *cam)
  1483. {
  1484. struct i2c_board_info ov7670_info = {
  1485. .type = "ov7670",
  1486. .addr = 0x42 >> 1,
  1487. .platform_data = &sensor_cfg,
  1488. };
  1489. int ret;
  1490. /*
  1491. * Validate the requested buffer mode.
  1492. */
  1493. if (buffer_mode >= 0)
  1494. cam->buffer_mode = buffer_mode;
  1495. if (cam->buffer_mode == B_DMA_sg &&
  1496. cam->chip_id == V4L2_IDENT_CAFE) {
  1497. printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
  1498. "attempting vmalloc mode instead\n");
  1499. cam->buffer_mode = B_vmalloc;
  1500. }
  1501. if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
  1502. printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
  1503. cam->buffer_mode);
  1504. return -EINVAL;
  1505. }
  1506. /*
  1507. * Register with V4L
  1508. */
  1509. ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
  1510. if (ret)
  1511. return ret;
  1512. mutex_init(&cam->s_mutex);
  1513. cam->state = S_NOTREADY;
  1514. mcam_set_config_needed(cam, 1);
  1515. cam->pix_format = mcam_def_pix_format;
  1516. cam->mbus_code = mcam_def_mbus_code;
  1517. INIT_LIST_HEAD(&cam->buffers);
  1518. mcam_ctlr_init(cam);
  1519. /*
  1520. * Try to find the sensor.
  1521. */
  1522. sensor_cfg.clock_speed = cam->clock_speed;
  1523. sensor_cfg.use_smbus = cam->use_smbus;
  1524. cam->sensor_addr = ov7670_info.addr;
  1525. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
  1526. cam->i2c_adapter, &ov7670_info, NULL);
  1527. if (cam->sensor == NULL) {
  1528. ret = -ENODEV;
  1529. goto out_unregister;
  1530. }
  1531. ret = mcam_cam_init(cam);
  1532. if (ret)
  1533. goto out_unregister;
  1534. /*
  1535. * Get the v4l2 setup done.
  1536. */
  1537. mutex_lock(&cam->s_mutex);
  1538. cam->vdev = mcam_v4l_template;
  1539. cam->vdev.debug = 0;
  1540. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1541. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1542. if (ret)
  1543. goto out;
  1544. video_set_drvdata(&cam->vdev, cam);
  1545. /*
  1546. * If so requested, try to get our DMA buffers now.
  1547. */
  1548. if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
  1549. if (mcam_alloc_dma_bufs(cam, 1))
  1550. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1551. " will try again later.");
  1552. }
  1553. out:
  1554. mutex_unlock(&cam->s_mutex);
  1555. return ret;
  1556. out_unregister:
  1557. v4l2_device_unregister(&cam->v4l2_dev);
  1558. return ret;
  1559. }
  1560. void mccic_shutdown(struct mcam_camera *cam)
  1561. {
  1562. /*
  1563. * If we have no users (and we really, really should have no
  1564. * users) the device will already be powered down. Trying to
  1565. * take it down again will wedge the machine, which is frowned
  1566. * upon.
  1567. */
  1568. if (cam->users > 0) {
  1569. cam_warn(cam, "Removing a device with users!\n");
  1570. mcam_ctlr_power_down(cam);
  1571. }
  1572. vb2_queue_release(&cam->vb_queue);
  1573. if (cam->buffer_mode == B_vmalloc)
  1574. mcam_free_dma_bufs(cam);
  1575. video_unregister_device(&cam->vdev);
  1576. v4l2_device_unregister(&cam->v4l2_dev);
  1577. }
  1578. /*
  1579. * Power management
  1580. */
  1581. #ifdef CONFIG_PM
  1582. void mccic_suspend(struct mcam_camera *cam)
  1583. {
  1584. enum mcam_state cstate = cam->state;
  1585. mcam_ctlr_stop_dma(cam);
  1586. mcam_ctlr_power_down(cam);
  1587. cam->state = cstate;
  1588. }
  1589. int mccic_resume(struct mcam_camera *cam)
  1590. {
  1591. int ret = 0;
  1592. mutex_lock(&cam->s_mutex);
  1593. if (cam->users > 0) {
  1594. mcam_ctlr_power_up(cam);
  1595. __mcam_cam_reset(cam);
  1596. } else {
  1597. mcam_ctlr_power_down(cam);
  1598. }
  1599. mutex_unlock(&cam->s_mutex);
  1600. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1601. if (cam->state == S_STREAMING)
  1602. ret = mcam_read_setup(cam);
  1603. return ret;
  1604. }
  1605. #endif /* CONFIG_PM */