mcam-core.c 49 KB

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