mcam-core.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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 bool 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 bool 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 bool 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. /*
  450. * Very Bad Not Good Things happen if you don't clear
  451. * C1_DESC_ENA before making any descriptor changes.
  452. */
  453. mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
  454. mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
  455. mcam_reg_write(cam, REG_DESC_LEN_Y,
  456. buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
  457. mcam_reg_write(cam, REG_DESC_LEN_U, 0);
  458. mcam_reg_write(cam, REG_DESC_LEN_V, 0);
  459. mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
  460. cam->vb_bufs[0] = buf;
  461. }
  462. /*
  463. * Initial B_DMA_sg setup
  464. */
  465. static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
  466. {
  467. /*
  468. * The list-empty condition can hit us at resume time
  469. * if the buffer list was empty when the system was suspended.
  470. */
  471. if (list_empty(&cam->buffers)) {
  472. set_bit(CF_SG_RESTART, &cam->flags);
  473. return;
  474. }
  475. mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
  476. mcam_sg_next_buffer(cam);
  477. cam->nbufs = 3;
  478. }
  479. /*
  480. * Frame completion with S/G is trickier. We can't muck with
  481. * a descriptor chain on the fly, since the controller buffers it
  482. * internally. So we have to actually stop and restart; Marvell
  483. * says this is the way to do it.
  484. *
  485. * Of course, stopping is easier said than done; experience shows
  486. * that the controller can start a frame *after* C0_ENABLE has been
  487. * cleared. So when running in S/G mode, the controller is "stopped"
  488. * on receipt of the start-of-frame interrupt. That means we can
  489. * safely change the DMA descriptor array here and restart things
  490. * (assuming there's another buffer waiting to go).
  491. */
  492. static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
  493. {
  494. struct mcam_vb_buffer *buf = cam->vb_bufs[0];
  495. /*
  496. * If we're no longer supposed to be streaming, don't do anything.
  497. */
  498. if (cam->state != S_STREAMING)
  499. return;
  500. /*
  501. * If we have another buffer available, put it in and
  502. * restart the engine.
  503. */
  504. if (!list_empty(&cam->buffers)) {
  505. mcam_sg_next_buffer(cam);
  506. mcam_ctlr_start(cam);
  507. /*
  508. * Otherwise set CF_SG_RESTART and the controller will
  509. * be restarted once another buffer shows up.
  510. */
  511. } else {
  512. set_bit(CF_SG_RESTART, &cam->flags);
  513. singles++;
  514. cam->vb_bufs[0] = NULL;
  515. }
  516. /*
  517. * Now we can give the completed frame back to user space.
  518. */
  519. delivered++;
  520. mcam_buffer_done(cam, frame, &buf->vb_buf);
  521. }
  522. /*
  523. * Scatter/gather mode requires stopping the controller between
  524. * frames so we can put in a new DMA descriptor array. If no new
  525. * buffer exists at frame completion, the controller is left stopped;
  526. * this function is charged with gettig things going again.
  527. */
  528. static void mcam_sg_restart(struct mcam_camera *cam)
  529. {
  530. mcam_ctlr_dma_sg(cam);
  531. mcam_ctlr_start(cam);
  532. clear_bit(CF_SG_RESTART, &cam->flags);
  533. }
  534. #else /* MCAM_MODE_DMA_SG */
  535. static inline void mcam_sg_restart(struct mcam_camera *cam)
  536. {
  537. return;
  538. }
  539. #endif /* MCAM_MODE_DMA_SG */
  540. /* ---------------------------------------------------------------------- */
  541. /*
  542. * Buffer-mode-independent controller code.
  543. */
  544. /*
  545. * Image format setup
  546. */
  547. static void mcam_ctlr_image(struct mcam_camera *cam)
  548. {
  549. int imgsz;
  550. struct v4l2_pix_format *fmt = &cam->pix_format;
  551. imgsz = ((fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK) |
  552. (fmt->bytesperline & IMGSZ_H_MASK);
  553. mcam_reg_write(cam, REG_IMGSIZE, imgsz);
  554. mcam_reg_write(cam, REG_IMGOFFSET, 0);
  555. /* YPITCH just drops the last two bits */
  556. mcam_reg_write_mask(cam, REG_IMGPITCH, fmt->bytesperline,
  557. IMGP_YP_MASK);
  558. /*
  559. * Tell the controller about the image format we are using.
  560. */
  561. switch (cam->pix_format.pixelformat) {
  562. case V4L2_PIX_FMT_YUYV:
  563. mcam_reg_write_mask(cam, REG_CTRL0,
  564. C0_DF_YUV|C0_YUV_PACKED|C0_YUVE_YUYV,
  565. C0_DF_MASK);
  566. break;
  567. case V4L2_PIX_FMT_RGB444:
  568. mcam_reg_write_mask(cam, REG_CTRL0,
  569. C0_DF_RGB|C0_RGBF_444|C0_RGB4_XRGB,
  570. C0_DF_MASK);
  571. /* Alpha value? */
  572. break;
  573. case V4L2_PIX_FMT_RGB565:
  574. mcam_reg_write_mask(cam, REG_CTRL0,
  575. C0_DF_RGB|C0_RGBF_565|C0_RGB5_BGGR,
  576. C0_DF_MASK);
  577. break;
  578. default:
  579. cam_err(cam, "Unknown format %x\n", cam->pix_format.pixelformat);
  580. break;
  581. }
  582. /*
  583. * Make sure it knows we want to use hsync/vsync.
  584. */
  585. mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC,
  586. C0_SIFM_MASK);
  587. }
  588. /*
  589. * Configure the controller for operation; caller holds the
  590. * device mutex.
  591. */
  592. static int mcam_ctlr_configure(struct mcam_camera *cam)
  593. {
  594. unsigned long flags;
  595. spin_lock_irqsave(&cam->dev_lock, flags);
  596. clear_bit(CF_SG_RESTART, &cam->flags);
  597. cam->dma_setup(cam);
  598. mcam_ctlr_image(cam);
  599. mcam_set_config_needed(cam, 0);
  600. spin_unlock_irqrestore(&cam->dev_lock, flags);
  601. return 0;
  602. }
  603. static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
  604. {
  605. /*
  606. * Clear any pending interrupts, since we do not
  607. * expect to have I/O active prior to enabling.
  608. */
  609. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
  610. mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
  611. }
  612. static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
  613. {
  614. mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
  615. }
  616. static void mcam_ctlr_init(struct mcam_camera *cam)
  617. {
  618. unsigned long flags;
  619. spin_lock_irqsave(&cam->dev_lock, flags);
  620. /*
  621. * Make sure it's not powered down.
  622. */
  623. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  624. /*
  625. * Turn off the enable bit. It sure should be off anyway,
  626. * but it's good to be sure.
  627. */
  628. mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
  629. /*
  630. * Clock the sensor appropriately. Controller clock should
  631. * be 48MHz, sensor "typical" value is half that.
  632. */
  633. mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
  634. spin_unlock_irqrestore(&cam->dev_lock, flags);
  635. }
  636. /*
  637. * Stop the controller, and don't return until we're really sure that no
  638. * further DMA is going on.
  639. */
  640. static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
  641. {
  642. unsigned long flags;
  643. /*
  644. * Theory: stop the camera controller (whether it is operating
  645. * or not). Delay briefly just in case we race with the SOF
  646. * interrupt, then wait until no DMA is active.
  647. */
  648. spin_lock_irqsave(&cam->dev_lock, flags);
  649. clear_bit(CF_SG_RESTART, &cam->flags);
  650. mcam_ctlr_stop(cam);
  651. cam->state = S_IDLE;
  652. spin_unlock_irqrestore(&cam->dev_lock, flags);
  653. /*
  654. * This is a brutally long sleep, but experience shows that
  655. * it can take the controller a while to get the message that
  656. * it needs to stop grabbing frames. In particular, we can
  657. * sometimes (on mmp) get a frame at the end WITHOUT the
  658. * start-of-frame indication.
  659. */
  660. msleep(150);
  661. if (test_bit(CF_DMA_ACTIVE, &cam->flags))
  662. cam_err(cam, "Timeout waiting for DMA to end\n");
  663. /* This would be bad news - what now? */
  664. spin_lock_irqsave(&cam->dev_lock, flags);
  665. mcam_ctlr_irq_disable(cam);
  666. spin_unlock_irqrestore(&cam->dev_lock, flags);
  667. }
  668. /*
  669. * Power up and down.
  670. */
  671. static void mcam_ctlr_power_up(struct mcam_camera *cam)
  672. {
  673. unsigned long flags;
  674. spin_lock_irqsave(&cam->dev_lock, flags);
  675. cam->plat_power_up(cam);
  676. mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
  677. spin_unlock_irqrestore(&cam->dev_lock, flags);
  678. msleep(5); /* Just to be sure */
  679. }
  680. static void mcam_ctlr_power_down(struct mcam_camera *cam)
  681. {
  682. unsigned long flags;
  683. spin_lock_irqsave(&cam->dev_lock, flags);
  684. /*
  685. * School of hard knocks department: be sure we do any register
  686. * twiddling on the controller *before* calling the platform
  687. * power down routine.
  688. */
  689. mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
  690. cam->plat_power_down(cam);
  691. spin_unlock_irqrestore(&cam->dev_lock, flags);
  692. }
  693. /* -------------------------------------------------------------------- */
  694. /*
  695. * Communications with the sensor.
  696. */
  697. static int __mcam_cam_reset(struct mcam_camera *cam)
  698. {
  699. return sensor_call(cam, core, reset, 0);
  700. }
  701. /*
  702. * We have found the sensor on the i2c. Let's try to have a
  703. * conversation.
  704. */
  705. static int mcam_cam_init(struct mcam_camera *cam)
  706. {
  707. struct v4l2_dbg_chip_ident chip;
  708. int ret;
  709. mutex_lock(&cam->s_mutex);
  710. if (cam->state != S_NOTREADY)
  711. cam_warn(cam, "Cam init with device in funky state %d",
  712. cam->state);
  713. ret = __mcam_cam_reset(cam);
  714. if (ret)
  715. goto out;
  716. chip.ident = V4L2_IDENT_NONE;
  717. chip.match.type = V4L2_CHIP_MATCH_I2C_ADDR;
  718. chip.match.addr = cam->sensor_addr;
  719. ret = sensor_call(cam, core, g_chip_ident, &chip);
  720. if (ret)
  721. goto out;
  722. cam->sensor_type = chip.ident;
  723. if (cam->sensor_type != V4L2_IDENT_OV7670) {
  724. cam_err(cam, "Unsupported sensor type 0x%x", cam->sensor_type);
  725. ret = -EINVAL;
  726. goto out;
  727. }
  728. /* Get/set parameters? */
  729. ret = 0;
  730. cam->state = S_IDLE;
  731. out:
  732. mcam_ctlr_power_down(cam);
  733. mutex_unlock(&cam->s_mutex);
  734. return ret;
  735. }
  736. /*
  737. * Configure the sensor to match the parameters we have. Caller should
  738. * hold s_mutex
  739. */
  740. static int mcam_cam_set_flip(struct mcam_camera *cam)
  741. {
  742. struct v4l2_control ctrl;
  743. memset(&ctrl, 0, sizeof(ctrl));
  744. ctrl.id = V4L2_CID_VFLIP;
  745. ctrl.value = flip;
  746. return sensor_call(cam, core, s_ctrl, &ctrl);
  747. }
  748. static int mcam_cam_configure(struct mcam_camera *cam)
  749. {
  750. struct v4l2_mbus_framefmt mbus_fmt;
  751. int ret;
  752. v4l2_fill_mbus_format(&mbus_fmt, &cam->pix_format, cam->mbus_code);
  753. ret = sensor_call(cam, core, init, 0);
  754. if (ret == 0)
  755. ret = sensor_call(cam, video, s_mbus_fmt, &mbus_fmt);
  756. /*
  757. * OV7670 does weird things if flip is set *before* format...
  758. */
  759. ret += mcam_cam_set_flip(cam);
  760. return ret;
  761. }
  762. /*
  763. * Get everything ready, and start grabbing frames.
  764. */
  765. static int mcam_read_setup(struct mcam_camera *cam)
  766. {
  767. int ret;
  768. unsigned long flags;
  769. /*
  770. * Configuration. If we still don't have DMA buffers,
  771. * make one last, desperate attempt.
  772. */
  773. if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
  774. mcam_alloc_dma_bufs(cam, 0))
  775. return -ENOMEM;
  776. if (mcam_needs_config(cam)) {
  777. mcam_cam_configure(cam);
  778. ret = mcam_ctlr_configure(cam);
  779. if (ret)
  780. return ret;
  781. }
  782. /*
  783. * Turn it loose.
  784. */
  785. spin_lock_irqsave(&cam->dev_lock, flags);
  786. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  787. mcam_reset_buffers(cam);
  788. mcam_ctlr_irq_enable(cam);
  789. cam->state = S_STREAMING;
  790. if (!test_bit(CF_SG_RESTART, &cam->flags))
  791. mcam_ctlr_start(cam);
  792. spin_unlock_irqrestore(&cam->dev_lock, flags);
  793. return 0;
  794. }
  795. /* ----------------------------------------------------------------------- */
  796. /*
  797. * Videobuf2 interface code.
  798. */
  799. static int mcam_vb_queue_setup(struct vb2_queue *vq,
  800. const struct v4l2_format *fmt, unsigned int *nbufs,
  801. unsigned int *num_planes, unsigned int sizes[],
  802. void *alloc_ctxs[])
  803. {
  804. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  805. int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
  806. sizes[0] = cam->pix_format.sizeimage;
  807. *num_planes = 1; /* Someday we have to support planar formats... */
  808. if (*nbufs < minbufs)
  809. *nbufs = minbufs;
  810. if (cam->buffer_mode == B_DMA_contig)
  811. alloc_ctxs[0] = cam->vb_alloc_ctx;
  812. return 0;
  813. }
  814. static void mcam_vb_buf_queue(struct vb2_buffer *vb)
  815. {
  816. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  817. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  818. unsigned long flags;
  819. int start;
  820. spin_lock_irqsave(&cam->dev_lock, flags);
  821. start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
  822. list_add(&mvb->queue, &cam->buffers);
  823. if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
  824. mcam_sg_restart(cam);
  825. spin_unlock_irqrestore(&cam->dev_lock, flags);
  826. if (start)
  827. mcam_read_setup(cam);
  828. }
  829. /*
  830. * vb2 uses these to release the mutex when waiting in dqbuf. I'm
  831. * not actually sure we need to do this (I'm not sure that vb2_dqbuf() needs
  832. * to be called with the mutex held), but better safe than sorry.
  833. */
  834. static void mcam_vb_wait_prepare(struct vb2_queue *vq)
  835. {
  836. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  837. mutex_unlock(&cam->s_mutex);
  838. }
  839. static void mcam_vb_wait_finish(struct vb2_queue *vq)
  840. {
  841. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  842. mutex_lock(&cam->s_mutex);
  843. }
  844. /*
  845. * These need to be called with the mutex held from vb2
  846. */
  847. static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
  848. {
  849. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  850. if (cam->state != S_IDLE) {
  851. INIT_LIST_HEAD(&cam->buffers);
  852. return -EINVAL;
  853. }
  854. cam->sequence = 0;
  855. /*
  856. * Videobuf2 sneakily hoards all the buffers and won't
  857. * give them to us until *after* streaming starts. But
  858. * we can't actually start streaming until we have a
  859. * destination. So go into a wait state and hope they
  860. * give us buffers soon.
  861. */
  862. if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
  863. cam->state = S_BUFWAIT;
  864. return 0;
  865. }
  866. return mcam_read_setup(cam);
  867. }
  868. static int mcam_vb_stop_streaming(struct vb2_queue *vq)
  869. {
  870. struct mcam_camera *cam = vb2_get_drv_priv(vq);
  871. unsigned long flags;
  872. if (cam->state == S_BUFWAIT) {
  873. /* They never gave us buffers */
  874. cam->state = S_IDLE;
  875. return 0;
  876. }
  877. if (cam->state != S_STREAMING)
  878. return -EINVAL;
  879. mcam_ctlr_stop_dma(cam);
  880. /*
  881. * VB2 reclaims the buffers, so we need to forget
  882. * about them.
  883. */
  884. spin_lock_irqsave(&cam->dev_lock, flags);
  885. INIT_LIST_HEAD(&cam->buffers);
  886. spin_unlock_irqrestore(&cam->dev_lock, flags);
  887. return 0;
  888. }
  889. static const struct vb2_ops mcam_vb2_ops = {
  890. .queue_setup = mcam_vb_queue_setup,
  891. .buf_queue = mcam_vb_buf_queue,
  892. .start_streaming = mcam_vb_start_streaming,
  893. .stop_streaming = mcam_vb_stop_streaming,
  894. .wait_prepare = mcam_vb_wait_prepare,
  895. .wait_finish = mcam_vb_wait_finish,
  896. };
  897. #ifdef MCAM_MODE_DMA_SG
  898. /*
  899. * Scatter/gather mode uses all of the above functions plus a
  900. * few extras to deal with DMA mapping.
  901. */
  902. static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
  903. {
  904. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  905. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  906. int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
  907. mvb->dma_desc = dma_alloc_coherent(cam->dev,
  908. ndesc * sizeof(struct mcam_dma_desc),
  909. &mvb->dma_desc_pa, GFP_KERNEL);
  910. if (mvb->dma_desc == NULL) {
  911. cam_err(cam, "Unable to get DMA descriptor array\n");
  912. return -ENOMEM;
  913. }
  914. return 0;
  915. }
  916. static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
  917. {
  918. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  919. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  920. struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
  921. struct mcam_dma_desc *desc = mvb->dma_desc;
  922. struct scatterlist *sg;
  923. int i;
  924. mvb->dma_desc_nent = dma_map_sg(cam->dev, sgd->sglist, sgd->num_pages,
  925. DMA_FROM_DEVICE);
  926. if (mvb->dma_desc_nent <= 0)
  927. return -EIO; /* Not sure what's right here */
  928. for_each_sg(sgd->sglist, sg, mvb->dma_desc_nent, i) {
  929. desc->dma_addr = sg_dma_address(sg);
  930. desc->segment_len = sg_dma_len(sg);
  931. desc++;
  932. }
  933. return 0;
  934. }
  935. static int mcam_vb_sg_buf_finish(struct vb2_buffer *vb)
  936. {
  937. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  938. struct vb2_dma_sg_desc *sgd = vb2_dma_sg_plane_desc(vb, 0);
  939. dma_unmap_sg(cam->dev, sgd->sglist, sgd->num_pages, DMA_FROM_DEVICE);
  940. return 0;
  941. }
  942. static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
  943. {
  944. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  945. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  946. int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
  947. dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
  948. mvb->dma_desc, mvb->dma_desc_pa);
  949. }
  950. static const struct vb2_ops mcam_vb2_sg_ops = {
  951. .queue_setup = mcam_vb_queue_setup,
  952. .buf_init = mcam_vb_sg_buf_init,
  953. .buf_prepare = mcam_vb_sg_buf_prepare,
  954. .buf_queue = mcam_vb_buf_queue,
  955. .buf_finish = mcam_vb_sg_buf_finish,
  956. .buf_cleanup = mcam_vb_sg_buf_cleanup,
  957. .start_streaming = mcam_vb_start_streaming,
  958. .stop_streaming = mcam_vb_stop_streaming,
  959. .wait_prepare = mcam_vb_wait_prepare,
  960. .wait_finish = mcam_vb_wait_finish,
  961. };
  962. #endif /* MCAM_MODE_DMA_SG */
  963. static int mcam_setup_vb2(struct mcam_camera *cam)
  964. {
  965. struct vb2_queue *vq = &cam->vb_queue;
  966. memset(vq, 0, sizeof(*vq));
  967. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  968. vq->drv_priv = cam;
  969. INIT_LIST_HEAD(&cam->buffers);
  970. switch (cam->buffer_mode) {
  971. case B_DMA_contig:
  972. #ifdef MCAM_MODE_DMA_CONTIG
  973. vq->ops = &mcam_vb2_ops;
  974. vq->mem_ops = &vb2_dma_contig_memops;
  975. cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
  976. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  977. cam->dma_setup = mcam_ctlr_dma_contig;
  978. cam->frame_complete = mcam_dma_contig_done;
  979. #endif
  980. break;
  981. case B_DMA_sg:
  982. #ifdef MCAM_MODE_DMA_SG
  983. vq->ops = &mcam_vb2_sg_ops;
  984. vq->mem_ops = &vb2_dma_sg_memops;
  985. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  986. cam->dma_setup = mcam_ctlr_dma_sg;
  987. cam->frame_complete = mcam_dma_sg_done;
  988. #endif
  989. break;
  990. case B_vmalloc:
  991. #ifdef MCAM_MODE_VMALLOC
  992. tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
  993. (unsigned long) cam);
  994. vq->ops = &mcam_vb2_ops;
  995. vq->mem_ops = &vb2_vmalloc_memops;
  996. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  997. vq->io_modes = VB2_MMAP;
  998. cam->dma_setup = mcam_ctlr_dma_vmalloc;
  999. cam->frame_complete = mcam_vmalloc_done;
  1000. #endif
  1001. break;
  1002. }
  1003. return vb2_queue_init(vq);
  1004. }
  1005. static void mcam_cleanup_vb2(struct mcam_camera *cam)
  1006. {
  1007. vb2_queue_release(&cam->vb_queue);
  1008. #ifdef MCAM_MODE_DMA_CONTIG
  1009. if (cam->buffer_mode == B_DMA_contig)
  1010. vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
  1011. #endif
  1012. }
  1013. /* ---------------------------------------------------------------------- */
  1014. /*
  1015. * The long list of V4L2 ioctl() operations.
  1016. */
  1017. static int mcam_vidioc_streamon(struct file *filp, void *priv,
  1018. enum v4l2_buf_type type)
  1019. {
  1020. struct mcam_camera *cam = filp->private_data;
  1021. int ret;
  1022. mutex_lock(&cam->s_mutex);
  1023. ret = vb2_streamon(&cam->vb_queue, type);
  1024. mutex_unlock(&cam->s_mutex);
  1025. return ret;
  1026. }
  1027. static int mcam_vidioc_streamoff(struct file *filp, void *priv,
  1028. enum v4l2_buf_type type)
  1029. {
  1030. struct mcam_camera *cam = filp->private_data;
  1031. int ret;
  1032. mutex_lock(&cam->s_mutex);
  1033. ret = vb2_streamoff(&cam->vb_queue, type);
  1034. mutex_unlock(&cam->s_mutex);
  1035. return ret;
  1036. }
  1037. static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
  1038. struct v4l2_requestbuffers *req)
  1039. {
  1040. struct mcam_camera *cam = filp->private_data;
  1041. int ret;
  1042. mutex_lock(&cam->s_mutex);
  1043. ret = vb2_reqbufs(&cam->vb_queue, req);
  1044. mutex_unlock(&cam->s_mutex);
  1045. return ret;
  1046. }
  1047. static int mcam_vidioc_querybuf(struct file *filp, void *priv,
  1048. struct v4l2_buffer *buf)
  1049. {
  1050. struct mcam_camera *cam = filp->private_data;
  1051. int ret;
  1052. mutex_lock(&cam->s_mutex);
  1053. ret = vb2_querybuf(&cam->vb_queue, buf);
  1054. mutex_unlock(&cam->s_mutex);
  1055. return ret;
  1056. }
  1057. static int mcam_vidioc_qbuf(struct file *filp, void *priv,
  1058. struct v4l2_buffer *buf)
  1059. {
  1060. struct mcam_camera *cam = filp->private_data;
  1061. int ret;
  1062. mutex_lock(&cam->s_mutex);
  1063. ret = vb2_qbuf(&cam->vb_queue, buf);
  1064. mutex_unlock(&cam->s_mutex);
  1065. return ret;
  1066. }
  1067. static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
  1068. struct v4l2_buffer *buf)
  1069. {
  1070. struct mcam_camera *cam = filp->private_data;
  1071. int ret;
  1072. mutex_lock(&cam->s_mutex);
  1073. ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
  1074. mutex_unlock(&cam->s_mutex);
  1075. return ret;
  1076. }
  1077. static int mcam_vidioc_queryctrl(struct file *filp, void *priv,
  1078. struct v4l2_queryctrl *qc)
  1079. {
  1080. struct mcam_camera *cam = priv;
  1081. int ret;
  1082. mutex_lock(&cam->s_mutex);
  1083. ret = sensor_call(cam, core, queryctrl, qc);
  1084. mutex_unlock(&cam->s_mutex);
  1085. return ret;
  1086. }
  1087. static int mcam_vidioc_g_ctrl(struct file *filp, void *priv,
  1088. struct v4l2_control *ctrl)
  1089. {
  1090. struct mcam_camera *cam = priv;
  1091. int ret;
  1092. mutex_lock(&cam->s_mutex);
  1093. ret = sensor_call(cam, core, g_ctrl, ctrl);
  1094. mutex_unlock(&cam->s_mutex);
  1095. return ret;
  1096. }
  1097. static int mcam_vidioc_s_ctrl(struct file *filp, void *priv,
  1098. struct v4l2_control *ctrl)
  1099. {
  1100. struct mcam_camera *cam = priv;
  1101. int ret;
  1102. mutex_lock(&cam->s_mutex);
  1103. ret = sensor_call(cam, core, s_ctrl, ctrl);
  1104. mutex_unlock(&cam->s_mutex);
  1105. return ret;
  1106. }
  1107. static int mcam_vidioc_querycap(struct file *file, void *priv,
  1108. struct v4l2_capability *cap)
  1109. {
  1110. strcpy(cap->driver, "marvell_ccic");
  1111. strcpy(cap->card, "marvell_ccic");
  1112. cap->version = 1;
  1113. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1114. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1115. return 0;
  1116. }
  1117. static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
  1118. void *priv, struct v4l2_fmtdesc *fmt)
  1119. {
  1120. if (fmt->index >= N_MCAM_FMTS)
  1121. return -EINVAL;
  1122. strlcpy(fmt->description, mcam_formats[fmt->index].desc,
  1123. sizeof(fmt->description));
  1124. fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
  1125. return 0;
  1126. }
  1127. static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1128. struct v4l2_format *fmt)
  1129. {
  1130. struct mcam_camera *cam = priv;
  1131. struct mcam_format_struct *f;
  1132. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  1133. struct v4l2_mbus_framefmt mbus_fmt;
  1134. int ret;
  1135. f = mcam_find_format(pix->pixelformat);
  1136. pix->pixelformat = f->pixelformat;
  1137. v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
  1138. mutex_lock(&cam->s_mutex);
  1139. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  1140. mutex_unlock(&cam->s_mutex);
  1141. v4l2_fill_pix_format(pix, &mbus_fmt);
  1142. pix->bytesperline = pix->width * f->bpp;
  1143. pix->sizeimage = pix->height * pix->bytesperline;
  1144. return ret;
  1145. }
  1146. static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1147. struct v4l2_format *fmt)
  1148. {
  1149. struct mcam_camera *cam = priv;
  1150. struct mcam_format_struct *f;
  1151. int ret;
  1152. /*
  1153. * Can't do anything if the device is not idle
  1154. * Also can't if there are streaming buffers in place.
  1155. */
  1156. if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
  1157. return -EBUSY;
  1158. f = mcam_find_format(fmt->fmt.pix.pixelformat);
  1159. /*
  1160. * See if the formatting works in principle.
  1161. */
  1162. ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  1163. if (ret)
  1164. return ret;
  1165. /*
  1166. * Now we start to change things for real, so let's do it
  1167. * under lock.
  1168. */
  1169. mutex_lock(&cam->s_mutex);
  1170. cam->pix_format = fmt->fmt.pix;
  1171. cam->mbus_code = f->mbus_code;
  1172. /*
  1173. * Make sure we have appropriate DMA buffers.
  1174. */
  1175. if (cam->buffer_mode == B_vmalloc) {
  1176. ret = mcam_check_dma_buffers(cam);
  1177. if (ret)
  1178. goto out;
  1179. }
  1180. mcam_set_config_needed(cam, 1);
  1181. out:
  1182. mutex_unlock(&cam->s_mutex);
  1183. return ret;
  1184. }
  1185. /*
  1186. * Return our stored notion of how the camera is/should be configured.
  1187. * The V4l2 spec wants us to be smarter, and actually get this from
  1188. * the camera (and not mess with it at open time). Someday.
  1189. */
  1190. static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1191. struct v4l2_format *f)
  1192. {
  1193. struct mcam_camera *cam = priv;
  1194. f->fmt.pix = cam->pix_format;
  1195. return 0;
  1196. }
  1197. /*
  1198. * We only have one input - the sensor - so minimize the nonsense here.
  1199. */
  1200. static int mcam_vidioc_enum_input(struct file *filp, void *priv,
  1201. struct v4l2_input *input)
  1202. {
  1203. if (input->index != 0)
  1204. return -EINVAL;
  1205. input->type = V4L2_INPUT_TYPE_CAMERA;
  1206. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1207. strcpy(input->name, "Camera");
  1208. return 0;
  1209. }
  1210. static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1211. {
  1212. *i = 0;
  1213. return 0;
  1214. }
  1215. static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1216. {
  1217. if (i != 0)
  1218. return -EINVAL;
  1219. return 0;
  1220. }
  1221. /* from vivi.c */
  1222. static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  1223. {
  1224. return 0;
  1225. }
  1226. /*
  1227. * G/S_PARM. Most of this is done by the sensor, but we are
  1228. * the level which controls the number of read buffers.
  1229. */
  1230. static int mcam_vidioc_g_parm(struct file *filp, void *priv,
  1231. struct v4l2_streamparm *parms)
  1232. {
  1233. struct mcam_camera *cam = priv;
  1234. int ret;
  1235. mutex_lock(&cam->s_mutex);
  1236. ret = sensor_call(cam, video, g_parm, parms);
  1237. mutex_unlock(&cam->s_mutex);
  1238. parms->parm.capture.readbuffers = n_dma_bufs;
  1239. return ret;
  1240. }
  1241. static int mcam_vidioc_s_parm(struct file *filp, void *priv,
  1242. struct v4l2_streamparm *parms)
  1243. {
  1244. struct mcam_camera *cam = priv;
  1245. int ret;
  1246. mutex_lock(&cam->s_mutex);
  1247. ret = sensor_call(cam, video, s_parm, parms);
  1248. mutex_unlock(&cam->s_mutex);
  1249. parms->parm.capture.readbuffers = n_dma_bufs;
  1250. return ret;
  1251. }
  1252. static int mcam_vidioc_g_chip_ident(struct file *file, void *priv,
  1253. struct v4l2_dbg_chip_ident *chip)
  1254. {
  1255. struct mcam_camera *cam = priv;
  1256. chip->ident = V4L2_IDENT_NONE;
  1257. chip->revision = 0;
  1258. if (v4l2_chip_match_host(&chip->match)) {
  1259. chip->ident = cam->chip_id;
  1260. return 0;
  1261. }
  1262. return sensor_call(cam, core, g_chip_ident, chip);
  1263. }
  1264. static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
  1265. struct v4l2_frmsizeenum *sizes)
  1266. {
  1267. struct mcam_camera *cam = priv;
  1268. int ret;
  1269. mutex_lock(&cam->s_mutex);
  1270. ret = sensor_call(cam, video, enum_framesizes, sizes);
  1271. mutex_unlock(&cam->s_mutex);
  1272. return ret;
  1273. }
  1274. static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
  1275. struct v4l2_frmivalenum *interval)
  1276. {
  1277. struct mcam_camera *cam = priv;
  1278. int ret;
  1279. mutex_lock(&cam->s_mutex);
  1280. ret = sensor_call(cam, video, enum_frameintervals, interval);
  1281. mutex_unlock(&cam->s_mutex);
  1282. return ret;
  1283. }
  1284. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1285. static int mcam_vidioc_g_register(struct file *file, void *priv,
  1286. struct v4l2_dbg_register *reg)
  1287. {
  1288. struct mcam_camera *cam = priv;
  1289. if (v4l2_chip_match_host(&reg->match)) {
  1290. reg->val = mcam_reg_read(cam, reg->reg);
  1291. reg->size = 4;
  1292. return 0;
  1293. }
  1294. return sensor_call(cam, core, g_register, reg);
  1295. }
  1296. static int mcam_vidioc_s_register(struct file *file, void *priv,
  1297. struct v4l2_dbg_register *reg)
  1298. {
  1299. struct mcam_camera *cam = priv;
  1300. if (v4l2_chip_match_host(&reg->match)) {
  1301. mcam_reg_write(cam, reg->reg, reg->val);
  1302. return 0;
  1303. }
  1304. return sensor_call(cam, core, s_register, reg);
  1305. }
  1306. #endif
  1307. static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
  1308. .vidioc_querycap = mcam_vidioc_querycap,
  1309. .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
  1310. .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
  1311. .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
  1312. .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
  1313. .vidioc_enum_input = mcam_vidioc_enum_input,
  1314. .vidioc_g_input = mcam_vidioc_g_input,
  1315. .vidioc_s_input = mcam_vidioc_s_input,
  1316. .vidioc_s_std = mcam_vidioc_s_std,
  1317. .vidioc_reqbufs = mcam_vidioc_reqbufs,
  1318. .vidioc_querybuf = mcam_vidioc_querybuf,
  1319. .vidioc_qbuf = mcam_vidioc_qbuf,
  1320. .vidioc_dqbuf = mcam_vidioc_dqbuf,
  1321. .vidioc_streamon = mcam_vidioc_streamon,
  1322. .vidioc_streamoff = mcam_vidioc_streamoff,
  1323. .vidioc_queryctrl = mcam_vidioc_queryctrl,
  1324. .vidioc_g_ctrl = mcam_vidioc_g_ctrl,
  1325. .vidioc_s_ctrl = mcam_vidioc_s_ctrl,
  1326. .vidioc_g_parm = mcam_vidioc_g_parm,
  1327. .vidioc_s_parm = mcam_vidioc_s_parm,
  1328. .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
  1329. .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
  1330. .vidioc_g_chip_ident = mcam_vidioc_g_chip_ident,
  1331. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1332. .vidioc_g_register = mcam_vidioc_g_register,
  1333. .vidioc_s_register = mcam_vidioc_s_register,
  1334. #endif
  1335. };
  1336. /* ---------------------------------------------------------------------- */
  1337. /*
  1338. * Our various file operations.
  1339. */
  1340. static int mcam_v4l_open(struct file *filp)
  1341. {
  1342. struct mcam_camera *cam = video_drvdata(filp);
  1343. int ret = 0;
  1344. filp->private_data = cam;
  1345. frames = singles = delivered = 0;
  1346. mutex_lock(&cam->s_mutex);
  1347. if (cam->users == 0) {
  1348. ret = mcam_setup_vb2(cam);
  1349. if (ret)
  1350. goto out;
  1351. mcam_ctlr_power_up(cam);
  1352. __mcam_cam_reset(cam);
  1353. mcam_set_config_needed(cam, 1);
  1354. }
  1355. (cam->users)++;
  1356. out:
  1357. mutex_unlock(&cam->s_mutex);
  1358. return ret;
  1359. }
  1360. static int mcam_v4l_release(struct file *filp)
  1361. {
  1362. struct mcam_camera *cam = filp->private_data;
  1363. cam_dbg(cam, "Release, %d frames, %d singles, %d delivered\n", frames,
  1364. singles, delivered);
  1365. mutex_lock(&cam->s_mutex);
  1366. (cam->users)--;
  1367. if (cam->users == 0) {
  1368. mcam_ctlr_stop_dma(cam);
  1369. mcam_cleanup_vb2(cam);
  1370. mcam_ctlr_power_down(cam);
  1371. if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
  1372. mcam_free_dma_bufs(cam);
  1373. }
  1374. mutex_unlock(&cam->s_mutex);
  1375. return 0;
  1376. }
  1377. static ssize_t mcam_v4l_read(struct file *filp,
  1378. char __user *buffer, size_t len, loff_t *pos)
  1379. {
  1380. struct mcam_camera *cam = filp->private_data;
  1381. int ret;
  1382. mutex_lock(&cam->s_mutex);
  1383. ret = vb2_read(&cam->vb_queue, buffer, len, pos,
  1384. filp->f_flags & O_NONBLOCK);
  1385. mutex_unlock(&cam->s_mutex);
  1386. return ret;
  1387. }
  1388. static unsigned int mcam_v4l_poll(struct file *filp,
  1389. struct poll_table_struct *pt)
  1390. {
  1391. struct mcam_camera *cam = filp->private_data;
  1392. int ret;
  1393. mutex_lock(&cam->s_mutex);
  1394. ret = vb2_poll(&cam->vb_queue, filp, pt);
  1395. mutex_unlock(&cam->s_mutex);
  1396. return ret;
  1397. }
  1398. static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1399. {
  1400. struct mcam_camera *cam = filp->private_data;
  1401. int ret;
  1402. mutex_lock(&cam->s_mutex);
  1403. ret = vb2_mmap(&cam->vb_queue, vma);
  1404. mutex_unlock(&cam->s_mutex);
  1405. return ret;
  1406. }
  1407. static const struct v4l2_file_operations mcam_v4l_fops = {
  1408. .owner = THIS_MODULE,
  1409. .open = mcam_v4l_open,
  1410. .release = mcam_v4l_release,
  1411. .read = mcam_v4l_read,
  1412. .poll = mcam_v4l_poll,
  1413. .mmap = mcam_v4l_mmap,
  1414. .unlocked_ioctl = video_ioctl2,
  1415. };
  1416. /*
  1417. * This template device holds all of those v4l2 methods; we
  1418. * clone it for specific real devices.
  1419. */
  1420. static struct video_device mcam_v4l_template = {
  1421. .name = "mcam",
  1422. .tvnorms = V4L2_STD_NTSC_M,
  1423. .current_norm = V4L2_STD_NTSC_M, /* make mplayer happy */
  1424. .fops = &mcam_v4l_fops,
  1425. .ioctl_ops = &mcam_v4l_ioctl_ops,
  1426. .release = video_device_release_empty,
  1427. };
  1428. /* ---------------------------------------------------------------------- */
  1429. /*
  1430. * Interrupt handler stuff
  1431. */
  1432. static void mcam_frame_complete(struct mcam_camera *cam, int frame)
  1433. {
  1434. /*
  1435. * Basic frame housekeeping.
  1436. */
  1437. set_bit(frame, &cam->flags);
  1438. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1439. cam->next_buf = frame;
  1440. cam->buf_seq[frame] = ++(cam->sequence);
  1441. frames++;
  1442. /*
  1443. * "This should never happen"
  1444. */
  1445. if (cam->state != S_STREAMING)
  1446. return;
  1447. /*
  1448. * Process the frame and set up the next one.
  1449. */
  1450. cam->frame_complete(cam, frame);
  1451. }
  1452. /*
  1453. * The interrupt handler; this needs to be called from the
  1454. * platform irq handler with the lock held.
  1455. */
  1456. int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
  1457. {
  1458. unsigned int frame, handled = 0;
  1459. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1460. /*
  1461. * Handle any frame completions. There really should
  1462. * not be more than one of these, or we have fallen
  1463. * far behind.
  1464. *
  1465. * When running in S/G mode, the frame number lacks any
  1466. * real meaning - there's only one descriptor array - but
  1467. * the controller still picks a different one to signal
  1468. * each time.
  1469. */
  1470. for (frame = 0; frame < cam->nbufs; frame++)
  1471. if (irqs & (IRQ_EOF0 << frame)) {
  1472. mcam_frame_complete(cam, frame);
  1473. handled = 1;
  1474. if (cam->buffer_mode == B_DMA_sg)
  1475. break;
  1476. }
  1477. /*
  1478. * If a frame starts, note that we have DMA active. This
  1479. * code assumes that we won't get multiple frame interrupts
  1480. * at once; may want to rethink that.
  1481. */
  1482. if (irqs & (IRQ_SOF0 | IRQ_SOF1 | IRQ_SOF2)) {
  1483. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1484. handled = 1;
  1485. if (cam->buffer_mode == B_DMA_sg)
  1486. mcam_ctlr_stop(cam);
  1487. }
  1488. return handled;
  1489. }
  1490. /* ---------------------------------------------------------------------- */
  1491. /*
  1492. * Registration and such.
  1493. */
  1494. static struct ov7670_config sensor_cfg = {
  1495. /*
  1496. * Exclude QCIF mode, because it only captures a tiny portion
  1497. * of the sensor FOV
  1498. */
  1499. .min_width = 320,
  1500. .min_height = 240,
  1501. };
  1502. int mccic_register(struct mcam_camera *cam)
  1503. {
  1504. struct i2c_board_info ov7670_info = {
  1505. .type = "ov7670",
  1506. .addr = 0x42 >> 1,
  1507. .platform_data = &sensor_cfg,
  1508. };
  1509. int ret;
  1510. /*
  1511. * Validate the requested buffer mode.
  1512. */
  1513. if (buffer_mode >= 0)
  1514. cam->buffer_mode = buffer_mode;
  1515. if (cam->buffer_mode == B_DMA_sg &&
  1516. cam->chip_id == V4L2_IDENT_CAFE) {
  1517. printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
  1518. "attempting vmalloc mode instead\n");
  1519. cam->buffer_mode = B_vmalloc;
  1520. }
  1521. if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
  1522. printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
  1523. cam->buffer_mode);
  1524. return -EINVAL;
  1525. }
  1526. /*
  1527. * Register with V4L
  1528. */
  1529. ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
  1530. if (ret)
  1531. return ret;
  1532. mutex_init(&cam->s_mutex);
  1533. cam->state = S_NOTREADY;
  1534. mcam_set_config_needed(cam, 1);
  1535. cam->pix_format = mcam_def_pix_format;
  1536. cam->mbus_code = mcam_def_mbus_code;
  1537. INIT_LIST_HEAD(&cam->buffers);
  1538. mcam_ctlr_init(cam);
  1539. /*
  1540. * Try to find the sensor.
  1541. */
  1542. sensor_cfg.clock_speed = cam->clock_speed;
  1543. sensor_cfg.use_smbus = cam->use_smbus;
  1544. cam->sensor_addr = ov7670_info.addr;
  1545. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
  1546. cam->i2c_adapter, &ov7670_info, NULL);
  1547. if (cam->sensor == NULL) {
  1548. ret = -ENODEV;
  1549. goto out_unregister;
  1550. }
  1551. ret = mcam_cam_init(cam);
  1552. if (ret)
  1553. goto out_unregister;
  1554. /*
  1555. * Get the v4l2 setup done.
  1556. */
  1557. mutex_lock(&cam->s_mutex);
  1558. cam->vdev = mcam_v4l_template;
  1559. cam->vdev.debug = 0;
  1560. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1561. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1562. if (ret)
  1563. goto out;
  1564. video_set_drvdata(&cam->vdev, cam);
  1565. /*
  1566. * If so requested, try to get our DMA buffers now.
  1567. */
  1568. if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
  1569. if (mcam_alloc_dma_bufs(cam, 1))
  1570. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1571. " will try again later.");
  1572. }
  1573. out:
  1574. mutex_unlock(&cam->s_mutex);
  1575. return ret;
  1576. out_unregister:
  1577. v4l2_device_unregister(&cam->v4l2_dev);
  1578. return ret;
  1579. }
  1580. void mccic_shutdown(struct mcam_camera *cam)
  1581. {
  1582. /*
  1583. * If we have no users (and we really, really should have no
  1584. * users) the device will already be powered down. Trying to
  1585. * take it down again will wedge the machine, which is frowned
  1586. * upon.
  1587. */
  1588. if (cam->users > 0) {
  1589. cam_warn(cam, "Removing a device with users!\n");
  1590. mcam_ctlr_power_down(cam);
  1591. }
  1592. vb2_queue_release(&cam->vb_queue);
  1593. if (cam->buffer_mode == B_vmalloc)
  1594. mcam_free_dma_bufs(cam);
  1595. video_unregister_device(&cam->vdev);
  1596. v4l2_device_unregister(&cam->v4l2_dev);
  1597. }
  1598. /*
  1599. * Power management
  1600. */
  1601. #ifdef CONFIG_PM
  1602. void mccic_suspend(struct mcam_camera *cam)
  1603. {
  1604. mutex_lock(&cam->s_mutex);
  1605. if (cam->users > 0) {
  1606. enum mcam_state cstate = cam->state;
  1607. mcam_ctlr_stop_dma(cam);
  1608. mcam_ctlr_power_down(cam);
  1609. cam->state = cstate;
  1610. }
  1611. mutex_unlock(&cam->s_mutex);
  1612. }
  1613. int mccic_resume(struct mcam_camera *cam)
  1614. {
  1615. int ret = 0;
  1616. mutex_lock(&cam->s_mutex);
  1617. if (cam->users > 0) {
  1618. mcam_ctlr_power_up(cam);
  1619. __mcam_cam_reset(cam);
  1620. } else {
  1621. mcam_ctlr_power_down(cam);
  1622. }
  1623. mutex_unlock(&cam->s_mutex);
  1624. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1625. if (cam->state == S_STREAMING) {
  1626. /*
  1627. * If there was a buffer in the DMA engine at suspend
  1628. * time, put it back on the queue or we'll forget about it.
  1629. */
  1630. if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
  1631. list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
  1632. ret = mcam_read_setup(cam);
  1633. }
  1634. return ret;
  1635. }
  1636. #endif /* CONFIG_PM */