mcam-core.c 49 KB

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