mcam-core.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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 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. mvb->dma_desc_nent = dma_map_sg(cam->dev, sg_table->sgl,
  1085. sg_table->nents, DMA_FROM_DEVICE);
  1086. if (mvb->dma_desc_nent <= 0)
  1087. return -EIO; /* Not sure what's right here */
  1088. for_each_sg(sg_table->sgl, 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 sg_table *sg_table = vb2_dma_sg_plane_desc(vb, 0);
  1099. if (sg_table)
  1100. dma_unmap_sg(cam->dev, sg_table->sgl,
  1101. sg_table->nents, DMA_FROM_DEVICE);
  1102. return 0;
  1103. }
  1104. static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
  1105. {
  1106. struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
  1107. struct mcam_vb_buffer *mvb = vb_to_mvb(vb);
  1108. int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
  1109. dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
  1110. mvb->dma_desc, mvb->dma_desc_pa);
  1111. }
  1112. static const struct vb2_ops mcam_vb2_sg_ops = {
  1113. .queue_setup = mcam_vb_queue_setup,
  1114. .buf_init = mcam_vb_sg_buf_init,
  1115. .buf_prepare = mcam_vb_sg_buf_prepare,
  1116. .buf_queue = mcam_vb_buf_queue,
  1117. .buf_finish = mcam_vb_sg_buf_finish,
  1118. .buf_cleanup = mcam_vb_sg_buf_cleanup,
  1119. .start_streaming = mcam_vb_start_streaming,
  1120. .stop_streaming = mcam_vb_stop_streaming,
  1121. .wait_prepare = mcam_vb_wait_prepare,
  1122. .wait_finish = mcam_vb_wait_finish,
  1123. };
  1124. #endif /* MCAM_MODE_DMA_SG */
  1125. static int mcam_setup_vb2(struct mcam_camera *cam)
  1126. {
  1127. struct vb2_queue *vq = &cam->vb_queue;
  1128. memset(vq, 0, sizeof(*vq));
  1129. vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1130. vq->drv_priv = cam;
  1131. INIT_LIST_HEAD(&cam->buffers);
  1132. switch (cam->buffer_mode) {
  1133. case B_DMA_contig:
  1134. #ifdef MCAM_MODE_DMA_CONTIG
  1135. vq->ops = &mcam_vb2_ops;
  1136. vq->mem_ops = &vb2_dma_contig_memops;
  1137. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  1138. cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
  1139. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1140. cam->dma_setup = mcam_ctlr_dma_contig;
  1141. cam->frame_complete = mcam_dma_contig_done;
  1142. #endif
  1143. break;
  1144. case B_DMA_sg:
  1145. #ifdef MCAM_MODE_DMA_SG
  1146. vq->ops = &mcam_vb2_sg_ops;
  1147. vq->mem_ops = &vb2_dma_sg_memops;
  1148. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  1149. vq->io_modes = VB2_MMAP | VB2_USERPTR;
  1150. cam->dma_setup = mcam_ctlr_dma_sg;
  1151. cam->frame_complete = mcam_dma_sg_done;
  1152. #endif
  1153. break;
  1154. case B_vmalloc:
  1155. #ifdef MCAM_MODE_VMALLOC
  1156. tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
  1157. (unsigned long) cam);
  1158. vq->ops = &mcam_vb2_ops;
  1159. vq->mem_ops = &vb2_vmalloc_memops;
  1160. vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
  1161. vq->io_modes = VB2_MMAP;
  1162. cam->dma_setup = mcam_ctlr_dma_vmalloc;
  1163. cam->frame_complete = mcam_vmalloc_done;
  1164. #endif
  1165. break;
  1166. }
  1167. return vb2_queue_init(vq);
  1168. }
  1169. static void mcam_cleanup_vb2(struct mcam_camera *cam)
  1170. {
  1171. vb2_queue_release(&cam->vb_queue);
  1172. #ifdef MCAM_MODE_DMA_CONTIG
  1173. if (cam->buffer_mode == B_DMA_contig)
  1174. vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
  1175. #endif
  1176. }
  1177. /* ---------------------------------------------------------------------- */
  1178. /*
  1179. * The long list of V4L2 ioctl() operations.
  1180. */
  1181. static int mcam_vidioc_streamon(struct file *filp, void *priv,
  1182. enum v4l2_buf_type type)
  1183. {
  1184. struct mcam_camera *cam = filp->private_data;
  1185. int ret;
  1186. mutex_lock(&cam->s_mutex);
  1187. ret = vb2_streamon(&cam->vb_queue, type);
  1188. mutex_unlock(&cam->s_mutex);
  1189. return ret;
  1190. }
  1191. static int mcam_vidioc_streamoff(struct file *filp, void *priv,
  1192. enum v4l2_buf_type type)
  1193. {
  1194. struct mcam_camera *cam = filp->private_data;
  1195. int ret;
  1196. mutex_lock(&cam->s_mutex);
  1197. ret = vb2_streamoff(&cam->vb_queue, type);
  1198. mutex_unlock(&cam->s_mutex);
  1199. return ret;
  1200. }
  1201. static int mcam_vidioc_reqbufs(struct file *filp, void *priv,
  1202. struct v4l2_requestbuffers *req)
  1203. {
  1204. struct mcam_camera *cam = filp->private_data;
  1205. int ret;
  1206. mutex_lock(&cam->s_mutex);
  1207. ret = vb2_reqbufs(&cam->vb_queue, req);
  1208. mutex_unlock(&cam->s_mutex);
  1209. return ret;
  1210. }
  1211. static int mcam_vidioc_querybuf(struct file *filp, void *priv,
  1212. struct v4l2_buffer *buf)
  1213. {
  1214. struct mcam_camera *cam = filp->private_data;
  1215. int ret;
  1216. mutex_lock(&cam->s_mutex);
  1217. ret = vb2_querybuf(&cam->vb_queue, buf);
  1218. mutex_unlock(&cam->s_mutex);
  1219. return ret;
  1220. }
  1221. static int mcam_vidioc_qbuf(struct file *filp, void *priv,
  1222. struct v4l2_buffer *buf)
  1223. {
  1224. struct mcam_camera *cam = filp->private_data;
  1225. int ret;
  1226. mutex_lock(&cam->s_mutex);
  1227. ret = vb2_qbuf(&cam->vb_queue, buf);
  1228. mutex_unlock(&cam->s_mutex);
  1229. return ret;
  1230. }
  1231. static int mcam_vidioc_dqbuf(struct file *filp, void *priv,
  1232. struct v4l2_buffer *buf)
  1233. {
  1234. struct mcam_camera *cam = filp->private_data;
  1235. int ret;
  1236. mutex_lock(&cam->s_mutex);
  1237. ret = vb2_dqbuf(&cam->vb_queue, buf, filp->f_flags & O_NONBLOCK);
  1238. mutex_unlock(&cam->s_mutex);
  1239. return ret;
  1240. }
  1241. static int mcam_vidioc_querycap(struct file *file, void *priv,
  1242. struct v4l2_capability *cap)
  1243. {
  1244. strcpy(cap->driver, "marvell_ccic");
  1245. strcpy(cap->card, "marvell_ccic");
  1246. cap->version = 1;
  1247. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  1248. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1249. return 0;
  1250. }
  1251. static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
  1252. void *priv, struct v4l2_fmtdesc *fmt)
  1253. {
  1254. if (fmt->index >= N_MCAM_FMTS)
  1255. return -EINVAL;
  1256. strlcpy(fmt->description, mcam_formats[fmt->index].desc,
  1257. sizeof(fmt->description));
  1258. fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
  1259. return 0;
  1260. }
  1261. static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
  1262. struct v4l2_format *fmt)
  1263. {
  1264. struct mcam_camera *cam = priv;
  1265. struct mcam_format_struct *f;
  1266. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  1267. struct v4l2_mbus_framefmt mbus_fmt;
  1268. int ret;
  1269. f = mcam_find_format(pix->pixelformat);
  1270. pix->pixelformat = f->pixelformat;
  1271. v4l2_fill_mbus_format(&mbus_fmt, pix, f->mbus_code);
  1272. mutex_lock(&cam->s_mutex);
  1273. ret = sensor_call(cam, video, try_mbus_fmt, &mbus_fmt);
  1274. mutex_unlock(&cam->s_mutex);
  1275. v4l2_fill_pix_format(pix, &mbus_fmt);
  1276. switch (f->pixelformat) {
  1277. case V4L2_PIX_FMT_YUV420:
  1278. case V4L2_PIX_FMT_YVU420:
  1279. pix->bytesperline = pix->width * 3 / 2;
  1280. break;
  1281. default:
  1282. pix->bytesperline = pix->width * f->bpp;
  1283. break;
  1284. }
  1285. pix->sizeimage = pix->height * pix->bytesperline;
  1286. return ret;
  1287. }
  1288. static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
  1289. struct v4l2_format *fmt)
  1290. {
  1291. struct mcam_camera *cam = priv;
  1292. struct mcam_format_struct *f;
  1293. int ret;
  1294. /*
  1295. * Can't do anything if the device is not idle
  1296. * Also can't if there are streaming buffers in place.
  1297. */
  1298. if (cam->state != S_IDLE || cam->vb_queue.num_buffers > 0)
  1299. return -EBUSY;
  1300. f = mcam_find_format(fmt->fmt.pix.pixelformat);
  1301. /*
  1302. * See if the formatting works in principle.
  1303. */
  1304. ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
  1305. if (ret)
  1306. return ret;
  1307. /*
  1308. * Now we start to change things for real, so let's do it
  1309. * under lock.
  1310. */
  1311. mutex_lock(&cam->s_mutex);
  1312. cam->pix_format = fmt->fmt.pix;
  1313. cam->mbus_code = f->mbus_code;
  1314. /*
  1315. * Make sure we have appropriate DMA buffers.
  1316. */
  1317. if (cam->buffer_mode == B_vmalloc) {
  1318. ret = mcam_check_dma_buffers(cam);
  1319. if (ret)
  1320. goto out;
  1321. }
  1322. mcam_set_config_needed(cam, 1);
  1323. out:
  1324. mutex_unlock(&cam->s_mutex);
  1325. return ret;
  1326. }
  1327. /*
  1328. * Return our stored notion of how the camera is/should be configured.
  1329. * The V4l2 spec wants us to be smarter, and actually get this from
  1330. * the camera (and not mess with it at open time). Someday.
  1331. */
  1332. static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
  1333. struct v4l2_format *f)
  1334. {
  1335. struct mcam_camera *cam = priv;
  1336. f->fmt.pix = cam->pix_format;
  1337. return 0;
  1338. }
  1339. /*
  1340. * We only have one input - the sensor - so minimize the nonsense here.
  1341. */
  1342. static int mcam_vidioc_enum_input(struct file *filp, void *priv,
  1343. struct v4l2_input *input)
  1344. {
  1345. if (input->index != 0)
  1346. return -EINVAL;
  1347. input->type = V4L2_INPUT_TYPE_CAMERA;
  1348. input->std = V4L2_STD_ALL; /* Not sure what should go here */
  1349. strcpy(input->name, "Camera");
  1350. return 0;
  1351. }
  1352. static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  1353. {
  1354. *i = 0;
  1355. return 0;
  1356. }
  1357. static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  1358. {
  1359. if (i != 0)
  1360. return -EINVAL;
  1361. return 0;
  1362. }
  1363. /* from vivi.c */
  1364. static int mcam_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id a)
  1365. {
  1366. return 0;
  1367. }
  1368. static int mcam_vidioc_g_std(struct file *filp, void *priv, v4l2_std_id *a)
  1369. {
  1370. *a = V4L2_STD_NTSC_M;
  1371. return 0;
  1372. }
  1373. /*
  1374. * G/S_PARM. Most of this is done by the sensor, but we are
  1375. * the level which controls the number of read buffers.
  1376. */
  1377. static int mcam_vidioc_g_parm(struct file *filp, void *priv,
  1378. struct v4l2_streamparm *parms)
  1379. {
  1380. struct mcam_camera *cam = priv;
  1381. int ret;
  1382. mutex_lock(&cam->s_mutex);
  1383. ret = sensor_call(cam, video, g_parm, parms);
  1384. mutex_unlock(&cam->s_mutex);
  1385. parms->parm.capture.readbuffers = n_dma_bufs;
  1386. return ret;
  1387. }
  1388. static int mcam_vidioc_s_parm(struct file *filp, void *priv,
  1389. struct v4l2_streamparm *parms)
  1390. {
  1391. struct mcam_camera *cam = priv;
  1392. int ret;
  1393. mutex_lock(&cam->s_mutex);
  1394. ret = sensor_call(cam, video, s_parm, parms);
  1395. mutex_unlock(&cam->s_mutex);
  1396. parms->parm.capture.readbuffers = n_dma_bufs;
  1397. return ret;
  1398. }
  1399. static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
  1400. struct v4l2_frmsizeenum *sizes)
  1401. {
  1402. struct mcam_camera *cam = priv;
  1403. int ret;
  1404. mutex_lock(&cam->s_mutex);
  1405. ret = sensor_call(cam, video, enum_framesizes, sizes);
  1406. mutex_unlock(&cam->s_mutex);
  1407. return ret;
  1408. }
  1409. static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
  1410. struct v4l2_frmivalenum *interval)
  1411. {
  1412. struct mcam_camera *cam = priv;
  1413. int ret;
  1414. mutex_lock(&cam->s_mutex);
  1415. ret = sensor_call(cam, video, enum_frameintervals, interval);
  1416. mutex_unlock(&cam->s_mutex);
  1417. return ret;
  1418. }
  1419. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1420. static int mcam_vidioc_g_register(struct file *file, void *priv,
  1421. struct v4l2_dbg_register *reg)
  1422. {
  1423. struct mcam_camera *cam = priv;
  1424. if (reg->reg > cam->regs_size - 4)
  1425. return -EINVAL;
  1426. reg->val = mcam_reg_read(cam, reg->reg);
  1427. reg->size = 4;
  1428. return 0;
  1429. }
  1430. static int mcam_vidioc_s_register(struct file *file, void *priv,
  1431. const struct v4l2_dbg_register *reg)
  1432. {
  1433. struct mcam_camera *cam = priv;
  1434. if (reg->reg > cam->regs_size - 4)
  1435. return -EINVAL;
  1436. mcam_reg_write(cam, reg->reg, reg->val);
  1437. return 0;
  1438. }
  1439. #endif
  1440. static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
  1441. .vidioc_querycap = mcam_vidioc_querycap,
  1442. .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
  1443. .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
  1444. .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
  1445. .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
  1446. .vidioc_enum_input = mcam_vidioc_enum_input,
  1447. .vidioc_g_input = mcam_vidioc_g_input,
  1448. .vidioc_s_input = mcam_vidioc_s_input,
  1449. .vidioc_s_std = mcam_vidioc_s_std,
  1450. .vidioc_g_std = mcam_vidioc_g_std,
  1451. .vidioc_reqbufs = mcam_vidioc_reqbufs,
  1452. .vidioc_querybuf = mcam_vidioc_querybuf,
  1453. .vidioc_qbuf = mcam_vidioc_qbuf,
  1454. .vidioc_dqbuf = mcam_vidioc_dqbuf,
  1455. .vidioc_streamon = mcam_vidioc_streamon,
  1456. .vidioc_streamoff = mcam_vidioc_streamoff,
  1457. .vidioc_g_parm = mcam_vidioc_g_parm,
  1458. .vidioc_s_parm = mcam_vidioc_s_parm,
  1459. .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
  1460. .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
  1461. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1462. .vidioc_g_register = mcam_vidioc_g_register,
  1463. .vidioc_s_register = mcam_vidioc_s_register,
  1464. #endif
  1465. };
  1466. /* ---------------------------------------------------------------------- */
  1467. /*
  1468. * Our various file operations.
  1469. */
  1470. static int mcam_v4l_open(struct file *filp)
  1471. {
  1472. struct mcam_camera *cam = video_drvdata(filp);
  1473. int ret = 0;
  1474. filp->private_data = cam;
  1475. cam->frame_state.frames = 0;
  1476. cam->frame_state.singles = 0;
  1477. cam->frame_state.delivered = 0;
  1478. mutex_lock(&cam->s_mutex);
  1479. if (cam->users == 0) {
  1480. ret = mcam_setup_vb2(cam);
  1481. if (ret)
  1482. goto out;
  1483. ret = mcam_ctlr_power_up(cam);
  1484. if (ret)
  1485. goto out;
  1486. __mcam_cam_reset(cam);
  1487. mcam_set_config_needed(cam, 1);
  1488. }
  1489. (cam->users)++;
  1490. out:
  1491. mutex_unlock(&cam->s_mutex);
  1492. return ret;
  1493. }
  1494. static int mcam_v4l_release(struct file *filp)
  1495. {
  1496. struct mcam_camera *cam = filp->private_data;
  1497. cam_dbg(cam, "Release, %d frames, %d singles, %d delivered\n",
  1498. cam->frame_state.frames, cam->frame_state.singles,
  1499. cam->frame_state.delivered);
  1500. mutex_lock(&cam->s_mutex);
  1501. (cam->users)--;
  1502. if (cam->users == 0) {
  1503. mcam_ctlr_stop_dma(cam);
  1504. mcam_cleanup_vb2(cam);
  1505. mcam_disable_mipi(cam);
  1506. mcam_ctlr_power_down(cam);
  1507. if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
  1508. mcam_free_dma_bufs(cam);
  1509. }
  1510. mutex_unlock(&cam->s_mutex);
  1511. return 0;
  1512. }
  1513. static ssize_t mcam_v4l_read(struct file *filp,
  1514. char __user *buffer, size_t len, loff_t *pos)
  1515. {
  1516. struct mcam_camera *cam = filp->private_data;
  1517. int ret;
  1518. mutex_lock(&cam->s_mutex);
  1519. ret = vb2_read(&cam->vb_queue, buffer, len, pos,
  1520. filp->f_flags & O_NONBLOCK);
  1521. mutex_unlock(&cam->s_mutex);
  1522. return ret;
  1523. }
  1524. static unsigned int mcam_v4l_poll(struct file *filp,
  1525. struct poll_table_struct *pt)
  1526. {
  1527. struct mcam_camera *cam = filp->private_data;
  1528. int ret;
  1529. mutex_lock(&cam->s_mutex);
  1530. ret = vb2_poll(&cam->vb_queue, filp, pt);
  1531. mutex_unlock(&cam->s_mutex);
  1532. return ret;
  1533. }
  1534. static int mcam_v4l_mmap(struct file *filp, struct vm_area_struct *vma)
  1535. {
  1536. struct mcam_camera *cam = filp->private_data;
  1537. int ret;
  1538. mutex_lock(&cam->s_mutex);
  1539. ret = vb2_mmap(&cam->vb_queue, vma);
  1540. mutex_unlock(&cam->s_mutex);
  1541. return ret;
  1542. }
  1543. static const struct v4l2_file_operations mcam_v4l_fops = {
  1544. .owner = THIS_MODULE,
  1545. .open = mcam_v4l_open,
  1546. .release = mcam_v4l_release,
  1547. .read = mcam_v4l_read,
  1548. .poll = mcam_v4l_poll,
  1549. .mmap = mcam_v4l_mmap,
  1550. .unlocked_ioctl = video_ioctl2,
  1551. };
  1552. /*
  1553. * This template device holds all of those v4l2 methods; we
  1554. * clone it for specific real devices.
  1555. */
  1556. static struct video_device mcam_v4l_template = {
  1557. .name = "mcam",
  1558. .tvnorms = V4L2_STD_NTSC_M,
  1559. .fops = &mcam_v4l_fops,
  1560. .ioctl_ops = &mcam_v4l_ioctl_ops,
  1561. .release = video_device_release_empty,
  1562. };
  1563. /* ---------------------------------------------------------------------- */
  1564. /*
  1565. * Interrupt handler stuff
  1566. */
  1567. static void mcam_frame_complete(struct mcam_camera *cam, int frame)
  1568. {
  1569. /*
  1570. * Basic frame housekeeping.
  1571. */
  1572. set_bit(frame, &cam->flags);
  1573. clear_bit(CF_DMA_ACTIVE, &cam->flags);
  1574. cam->next_buf = frame;
  1575. cam->buf_seq[frame] = ++(cam->sequence);
  1576. cam->frame_state.frames++;
  1577. /*
  1578. * "This should never happen"
  1579. */
  1580. if (cam->state != S_STREAMING)
  1581. return;
  1582. /*
  1583. * Process the frame and set up the next one.
  1584. */
  1585. cam->frame_complete(cam, frame);
  1586. }
  1587. /*
  1588. * The interrupt handler; this needs to be called from the
  1589. * platform irq handler with the lock held.
  1590. */
  1591. int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
  1592. {
  1593. unsigned int frame, handled = 0;
  1594. mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
  1595. /*
  1596. * Handle any frame completions. There really should
  1597. * not be more than one of these, or we have fallen
  1598. * far behind.
  1599. *
  1600. * When running in S/G mode, the frame number lacks any
  1601. * real meaning - there's only one descriptor array - but
  1602. * the controller still picks a different one to signal
  1603. * each time.
  1604. */
  1605. for (frame = 0; frame < cam->nbufs; frame++)
  1606. if (irqs & (IRQ_EOF0 << frame) &&
  1607. test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) {
  1608. mcam_frame_complete(cam, frame);
  1609. handled = 1;
  1610. clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
  1611. if (cam->buffer_mode == B_DMA_sg)
  1612. break;
  1613. }
  1614. /*
  1615. * If a frame starts, note that we have DMA active. This
  1616. * code assumes that we won't get multiple frame interrupts
  1617. * at once; may want to rethink that.
  1618. */
  1619. for (frame = 0; frame < cam->nbufs; frame++) {
  1620. if (irqs & (IRQ_SOF0 << frame)) {
  1621. set_bit(CF_FRAME_SOF0 + frame, &cam->flags);
  1622. handled = IRQ_HANDLED;
  1623. }
  1624. }
  1625. if (handled == IRQ_HANDLED) {
  1626. set_bit(CF_DMA_ACTIVE, &cam->flags);
  1627. if (cam->buffer_mode == B_DMA_sg)
  1628. mcam_ctlr_stop(cam);
  1629. }
  1630. return handled;
  1631. }
  1632. /* ---------------------------------------------------------------------- */
  1633. /*
  1634. * Registration and such.
  1635. */
  1636. static struct ov7670_config sensor_cfg = {
  1637. /*
  1638. * Exclude QCIF mode, because it only captures a tiny portion
  1639. * of the sensor FOV
  1640. */
  1641. .min_width = 320,
  1642. .min_height = 240,
  1643. };
  1644. int mccic_register(struct mcam_camera *cam)
  1645. {
  1646. struct i2c_board_info ov7670_info = {
  1647. .type = "ov7670",
  1648. .addr = 0x42 >> 1,
  1649. .platform_data = &sensor_cfg,
  1650. };
  1651. int ret;
  1652. /*
  1653. * Validate the requested buffer mode.
  1654. */
  1655. if (buffer_mode >= 0)
  1656. cam->buffer_mode = buffer_mode;
  1657. if (cam->buffer_mode == B_DMA_sg &&
  1658. cam->chip_id == MCAM_CAFE) {
  1659. printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
  1660. "attempting vmalloc mode instead\n");
  1661. cam->buffer_mode = B_vmalloc;
  1662. }
  1663. if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
  1664. printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
  1665. cam->buffer_mode);
  1666. return -EINVAL;
  1667. }
  1668. /*
  1669. * Register with V4L
  1670. */
  1671. ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
  1672. if (ret)
  1673. return ret;
  1674. mutex_init(&cam->s_mutex);
  1675. cam->state = S_NOTREADY;
  1676. mcam_set_config_needed(cam, 1);
  1677. cam->pix_format = mcam_def_pix_format;
  1678. cam->mbus_code = mcam_def_mbus_code;
  1679. INIT_LIST_HEAD(&cam->buffers);
  1680. mcam_ctlr_init(cam);
  1681. /*
  1682. * Try to find the sensor.
  1683. */
  1684. sensor_cfg.clock_speed = cam->clock_speed;
  1685. sensor_cfg.use_smbus = cam->use_smbus;
  1686. cam->sensor_addr = ov7670_info.addr;
  1687. cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
  1688. cam->i2c_adapter, &ov7670_info, NULL);
  1689. if (cam->sensor == NULL) {
  1690. ret = -ENODEV;
  1691. goto out_unregister;
  1692. }
  1693. ret = mcam_cam_init(cam);
  1694. if (ret)
  1695. goto out_unregister;
  1696. /*
  1697. * Get the v4l2 setup done.
  1698. */
  1699. ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
  1700. if (ret)
  1701. goto out_unregister;
  1702. cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
  1703. mutex_lock(&cam->s_mutex);
  1704. cam->vdev = mcam_v4l_template;
  1705. cam->vdev.debug = 0;
  1706. cam->vdev.v4l2_dev = &cam->v4l2_dev;
  1707. video_set_drvdata(&cam->vdev, cam);
  1708. ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
  1709. if (ret)
  1710. goto out;
  1711. /*
  1712. * If so requested, try to get our DMA buffers now.
  1713. */
  1714. if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
  1715. if (mcam_alloc_dma_bufs(cam, 1))
  1716. cam_warn(cam, "Unable to alloc DMA buffers at load"
  1717. " will try again later.");
  1718. }
  1719. out:
  1720. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1721. mutex_unlock(&cam->s_mutex);
  1722. return ret;
  1723. out_unregister:
  1724. v4l2_device_unregister(&cam->v4l2_dev);
  1725. return ret;
  1726. }
  1727. void mccic_shutdown(struct mcam_camera *cam)
  1728. {
  1729. /*
  1730. * If we have no users (and we really, really should have no
  1731. * users) the device will already be powered down. Trying to
  1732. * take it down again will wedge the machine, which is frowned
  1733. * upon.
  1734. */
  1735. if (cam->users > 0) {
  1736. cam_warn(cam, "Removing a device with users!\n");
  1737. mcam_ctlr_power_down(cam);
  1738. }
  1739. vb2_queue_release(&cam->vb_queue);
  1740. if (cam->buffer_mode == B_vmalloc)
  1741. mcam_free_dma_bufs(cam);
  1742. video_unregister_device(&cam->vdev);
  1743. v4l2_ctrl_handler_free(&cam->ctrl_handler);
  1744. v4l2_device_unregister(&cam->v4l2_dev);
  1745. }
  1746. /*
  1747. * Power management
  1748. */
  1749. #ifdef CONFIG_PM
  1750. void mccic_suspend(struct mcam_camera *cam)
  1751. {
  1752. mutex_lock(&cam->s_mutex);
  1753. if (cam->users > 0) {
  1754. enum mcam_state cstate = cam->state;
  1755. mcam_ctlr_stop_dma(cam);
  1756. mcam_ctlr_power_down(cam);
  1757. cam->state = cstate;
  1758. }
  1759. mutex_unlock(&cam->s_mutex);
  1760. }
  1761. int mccic_resume(struct mcam_camera *cam)
  1762. {
  1763. int ret = 0;
  1764. mutex_lock(&cam->s_mutex);
  1765. if (cam->users > 0) {
  1766. ret = mcam_ctlr_power_up(cam);
  1767. if (ret) {
  1768. mutex_unlock(&cam->s_mutex);
  1769. return ret;
  1770. }
  1771. __mcam_cam_reset(cam);
  1772. } else {
  1773. mcam_ctlr_power_down(cam);
  1774. }
  1775. mutex_unlock(&cam->s_mutex);
  1776. set_bit(CF_CONFIG_NEEDED, &cam->flags);
  1777. if (cam->state == S_STREAMING) {
  1778. /*
  1779. * If there was a buffer in the DMA engine at suspend
  1780. * time, put it back on the queue or we'll forget about it.
  1781. */
  1782. if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
  1783. list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
  1784. ret = mcam_read_setup(cam);
  1785. }
  1786. return ret;
  1787. }
  1788. #endif /* CONFIG_PM */