mcam-core.c 51 KB

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