pxa_camera.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  1. /*
  2. * V4L2 Driver for PXA camera host
  3. *
  4. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  5. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/io.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/fs.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/time.h>
  25. #include <linux/device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/clk.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-dev.h>
  32. #include <media/videobuf-dma-sg.h>
  33. #include <media/soc_camera.h>
  34. #include <media/soc_mediabus.h>
  35. #include <media/v4l2-of.h>
  36. #include <linux/videodev2.h>
  37. #include <mach/dma.h>
  38. #include <linux/platform_data/camera-pxa.h>
  39. #define PXA_CAM_VERSION "0.0.6"
  40. #define PXA_CAM_DRV_NAME "pxa27x-camera"
  41. /* Camera Interface */
  42. #define CICR0 0x0000
  43. #define CICR1 0x0004
  44. #define CICR2 0x0008
  45. #define CICR3 0x000C
  46. #define CICR4 0x0010
  47. #define CISR 0x0014
  48. #define CIFR 0x0018
  49. #define CITOR 0x001C
  50. #define CIBR0 0x0028
  51. #define CIBR1 0x0030
  52. #define CIBR2 0x0038
  53. #define CICR0_DMAEN (1 << 31) /* DMA request enable */
  54. #define CICR0_PAR_EN (1 << 30) /* Parity enable */
  55. #define CICR0_SL_CAP_EN (1 << 29) /* Capture enable for slave mode */
  56. #define CICR0_ENB (1 << 28) /* Camera interface enable */
  57. #define CICR0_DIS (1 << 27) /* Camera interface disable */
  58. #define CICR0_SIM (0x7 << 24) /* Sensor interface mode mask */
  59. #define CICR0_TOM (1 << 9) /* Time-out mask */
  60. #define CICR0_RDAVM (1 << 8) /* Receive-data-available mask */
  61. #define CICR0_FEM (1 << 7) /* FIFO-empty mask */
  62. #define CICR0_EOLM (1 << 6) /* End-of-line mask */
  63. #define CICR0_PERRM (1 << 5) /* Parity-error mask */
  64. #define CICR0_QDM (1 << 4) /* Quick-disable mask */
  65. #define CICR0_CDM (1 << 3) /* Disable-done mask */
  66. #define CICR0_SOFM (1 << 2) /* Start-of-frame mask */
  67. #define CICR0_EOFM (1 << 1) /* End-of-frame mask */
  68. #define CICR0_FOM (1 << 0) /* FIFO-overrun mask */
  69. #define CICR1_TBIT (1 << 31) /* Transparency bit */
  70. #define CICR1_RGBT_CONV (0x3 << 29) /* RGBT conversion mask */
  71. #define CICR1_PPL (0x7ff << 15) /* Pixels per line mask */
  72. #define CICR1_RGB_CONV (0x7 << 12) /* RGB conversion mask */
  73. #define CICR1_RGB_F (1 << 11) /* RGB format */
  74. #define CICR1_YCBCR_F (1 << 10) /* YCbCr format */
  75. #define CICR1_RGB_BPP (0x7 << 7) /* RGB bis per pixel mask */
  76. #define CICR1_RAW_BPP (0x3 << 5) /* Raw bis per pixel mask */
  77. #define CICR1_COLOR_SP (0x3 << 3) /* Color space mask */
  78. #define CICR1_DW (0x7 << 0) /* Data width mask */
  79. #define CICR2_BLW (0xff << 24) /* Beginning-of-line pixel clock
  80. wait count mask */
  81. #define CICR2_ELW (0xff << 16) /* End-of-line pixel clock
  82. wait count mask */
  83. #define CICR2_HSW (0x3f << 10) /* Horizontal sync pulse width mask */
  84. #define CICR2_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  85. wait count mask */
  86. #define CICR2_FSW (0x7 << 0) /* Frame stabilization
  87. wait count mask */
  88. #define CICR3_BFW (0xff << 24) /* Beginning-of-frame line clock
  89. wait count mask */
  90. #define CICR3_EFW (0xff << 16) /* End-of-frame line clock
  91. wait count mask */
  92. #define CICR3_VSW (0x3f << 10) /* Vertical sync pulse width mask */
  93. #define CICR3_BFPW (0x3f << 3) /* Beginning-of-frame pixel clock
  94. wait count mask */
  95. #define CICR3_LPF (0x7ff << 0) /* Lines per frame mask */
  96. #define CICR4_MCLK_DLY (0x3 << 24) /* MCLK Data Capture Delay mask */
  97. #define CICR4_PCLK_EN (1 << 23) /* Pixel clock enable */
  98. #define CICR4_PCP (1 << 22) /* Pixel clock polarity */
  99. #define CICR4_HSP (1 << 21) /* Horizontal sync polarity */
  100. #define CICR4_VSP (1 << 20) /* Vertical sync polarity */
  101. #define CICR4_MCLK_EN (1 << 19) /* MCLK enable */
  102. #define CICR4_FR_RATE (0x7 << 8) /* Frame rate mask */
  103. #define CICR4_DIV (0xff << 0) /* Clock divisor mask */
  104. #define CISR_FTO (1 << 15) /* FIFO time-out */
  105. #define CISR_RDAV_2 (1 << 14) /* Channel 2 receive data available */
  106. #define CISR_RDAV_1 (1 << 13) /* Channel 1 receive data available */
  107. #define CISR_RDAV_0 (1 << 12) /* Channel 0 receive data available */
  108. #define CISR_FEMPTY_2 (1 << 11) /* Channel 2 FIFO empty */
  109. #define CISR_FEMPTY_1 (1 << 10) /* Channel 1 FIFO empty */
  110. #define CISR_FEMPTY_0 (1 << 9) /* Channel 0 FIFO empty */
  111. #define CISR_EOL (1 << 8) /* End of line */
  112. #define CISR_PAR_ERR (1 << 7) /* Parity error */
  113. #define CISR_CQD (1 << 6) /* Camera interface quick disable */
  114. #define CISR_CDD (1 << 5) /* Camera interface disable done */
  115. #define CISR_SOF (1 << 4) /* Start of frame */
  116. #define CISR_EOF (1 << 3) /* End of frame */
  117. #define CISR_IFO_2 (1 << 2) /* FIFO overrun for Channel 2 */
  118. #define CISR_IFO_1 (1 << 1) /* FIFO overrun for Channel 1 */
  119. #define CISR_IFO_0 (1 << 0) /* FIFO overrun for Channel 0 */
  120. #define CIFR_FLVL2 (0x7f << 23) /* FIFO 2 level mask */
  121. #define CIFR_FLVL1 (0x7f << 16) /* FIFO 1 level mask */
  122. #define CIFR_FLVL0 (0xff << 8) /* FIFO 0 level mask */
  123. #define CIFR_THL_0 (0x3 << 4) /* Threshold Level for Channel 0 FIFO */
  124. #define CIFR_RESET_F (1 << 3) /* Reset input FIFOs */
  125. #define CIFR_FEN2 (1 << 2) /* FIFO enable for channel 2 */
  126. #define CIFR_FEN1 (1 << 1) /* FIFO enable for channel 1 */
  127. #define CIFR_FEN0 (1 << 0) /* FIFO enable for channel 0 */
  128. #define CICR0_SIM_MP (0 << 24)
  129. #define CICR0_SIM_SP (1 << 24)
  130. #define CICR0_SIM_MS (2 << 24)
  131. #define CICR0_SIM_EP (3 << 24)
  132. #define CICR0_SIM_ES (4 << 24)
  133. #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
  134. #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
  135. #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
  136. #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
  137. #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
  138. #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
  139. #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
  140. #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
  141. #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
  142. #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
  143. #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
  144. #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
  145. #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
  146. #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
  147. #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
  148. CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
  149. CICR0_EOFM | CICR0_FOM)
  150. /*
  151. * Structures
  152. */
  153. enum pxa_camera_active_dma {
  154. DMA_Y = 0x1,
  155. DMA_U = 0x2,
  156. DMA_V = 0x4,
  157. };
  158. /* descriptor needed for the PXA DMA engine */
  159. struct pxa_cam_dma {
  160. dma_addr_t sg_dma;
  161. struct pxa_dma_desc *sg_cpu;
  162. size_t sg_size;
  163. int sglen;
  164. };
  165. /* buffer for one video frame */
  166. struct pxa_buffer {
  167. /* common v4l buffer stuff -- must be first */
  168. struct videobuf_buffer vb;
  169. u32 code;
  170. /* our descriptor lists for Y, U and V channels */
  171. struct pxa_cam_dma dmas[3];
  172. int inwork;
  173. enum pxa_camera_active_dma active_dma;
  174. };
  175. struct pxa_camera_dev {
  176. struct soc_camera_host soc_host;
  177. /*
  178. * PXA27x is only supposed to handle one camera on its Quick Capture
  179. * interface. If anyone ever builds hardware to enable more than
  180. * one camera, they will have to modify this driver too
  181. */
  182. struct clk *clk;
  183. unsigned int irq;
  184. void __iomem *base;
  185. int channels;
  186. unsigned int dma_chans[3];
  187. struct pxacamera_platform_data *pdata;
  188. struct resource *res;
  189. unsigned long platform_flags;
  190. unsigned long ciclk;
  191. unsigned long mclk;
  192. u32 mclk_divisor;
  193. u16 width_flags; /* max 10 bits */
  194. struct list_head capture;
  195. spinlock_t lock;
  196. struct pxa_buffer *active;
  197. struct pxa_dma_desc *sg_tail[3];
  198. u32 save_cicr[5];
  199. };
  200. struct pxa_cam {
  201. unsigned long flags;
  202. };
  203. static const char *pxa_cam_driver_description = "PXA_Camera";
  204. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  205. /*
  206. * Videobuf operations
  207. */
  208. static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  209. unsigned int *size)
  210. {
  211. struct soc_camera_device *icd = vq->priv_data;
  212. dev_dbg(icd->parent, "count=%d, size=%d\n", *count, *size);
  213. *size = icd->sizeimage;
  214. if (0 == *count)
  215. *count = 32;
  216. if (*size * *count > vid_limit * 1024 * 1024)
  217. *count = (vid_limit * 1024 * 1024) / *size;
  218. return 0;
  219. }
  220. static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
  221. {
  222. struct soc_camera_device *icd = vq->priv_data;
  223. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  224. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  225. int i;
  226. BUG_ON(in_interrupt());
  227. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  228. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  229. /*
  230. * This waits until this buffer is out of danger, i.e., until it is no
  231. * longer in STATE_QUEUED or STATE_ACTIVE
  232. */
  233. videobuf_waiton(vq, &buf->vb, 0, 0);
  234. videobuf_dma_unmap(vq->dev, dma);
  235. videobuf_dma_free(dma);
  236. for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
  237. if (buf->dmas[i].sg_cpu)
  238. dma_free_coherent(ici->v4l2_dev.dev,
  239. buf->dmas[i].sg_size,
  240. buf->dmas[i].sg_cpu,
  241. buf->dmas[i].sg_dma);
  242. buf->dmas[i].sg_cpu = NULL;
  243. }
  244. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  245. }
  246. static int calculate_dma_sglen(struct scatterlist *sglist, int sglen,
  247. int sg_first_ofs, int size)
  248. {
  249. int i, offset, dma_len, xfer_len;
  250. struct scatterlist *sg;
  251. offset = sg_first_ofs;
  252. for_each_sg(sglist, sg, sglen, i) {
  253. dma_len = sg_dma_len(sg);
  254. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  255. xfer_len = roundup(min(dma_len - offset, size), 8);
  256. size = max(0, size - xfer_len);
  257. offset = 0;
  258. if (size == 0)
  259. break;
  260. }
  261. BUG_ON(size != 0);
  262. return i + 1;
  263. }
  264. /**
  265. * pxa_init_dma_channel - init dma descriptors
  266. * @pcdev: pxa camera device
  267. * @buf: pxa buffer to find pxa dma channel
  268. * @dma: dma video buffer
  269. * @channel: dma channel (0 => 'Y', 1 => 'U', 2 => 'V')
  270. * @cibr: camera Receive Buffer Register
  271. * @size: bytes to transfer
  272. * @sg_first: first element of sg_list
  273. * @sg_first_ofs: offset in first element of sg_list
  274. *
  275. * Prepares the pxa dma descriptors to transfer one camera channel.
  276. * Beware sg_first and sg_first_ofs are both input and output parameters.
  277. *
  278. * Returns 0 or -ENOMEM if no coherent memory is available
  279. */
  280. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  281. struct pxa_buffer *buf,
  282. struct videobuf_dmabuf *dma, int channel,
  283. int cibr, int size,
  284. struct scatterlist **sg_first, int *sg_first_ofs)
  285. {
  286. struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
  287. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  288. struct scatterlist *sg;
  289. int i, offset, sglen;
  290. int dma_len = 0, xfer_len = 0;
  291. if (pxa_dma->sg_cpu)
  292. dma_free_coherent(dev, pxa_dma->sg_size,
  293. pxa_dma->sg_cpu, pxa_dma->sg_dma);
  294. sglen = calculate_dma_sglen(*sg_first, dma->sglen,
  295. *sg_first_ofs, size);
  296. pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
  297. pxa_dma->sg_cpu = dma_alloc_coherent(dev, pxa_dma->sg_size,
  298. &pxa_dma->sg_dma, GFP_KERNEL);
  299. if (!pxa_dma->sg_cpu)
  300. return -ENOMEM;
  301. pxa_dma->sglen = sglen;
  302. offset = *sg_first_ofs;
  303. dev_dbg(dev, "DMA: sg_first=%p, sglen=%d, ofs=%d, dma.desc=%x\n",
  304. *sg_first, sglen, *sg_first_ofs, pxa_dma->sg_dma);
  305. for_each_sg(*sg_first, sg, sglen, i) {
  306. dma_len = sg_dma_len(sg);
  307. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  308. xfer_len = roundup(min(dma_len - offset, size), 8);
  309. size = max(0, size - xfer_len);
  310. pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
  311. pxa_dma->sg_cpu[i].dtadr = sg_dma_address(sg) + offset;
  312. pxa_dma->sg_cpu[i].dcmd =
  313. DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
  314. #ifdef DEBUG
  315. if (!i)
  316. pxa_dma->sg_cpu[i].dcmd |= DCMD_STARTIRQEN;
  317. #endif
  318. pxa_dma->sg_cpu[i].ddadr =
  319. pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
  320. dev_vdbg(dev, "DMA: desc.%08x->@phys=0x%08x, len=%d\n",
  321. pxa_dma->sg_dma + i * sizeof(struct pxa_dma_desc),
  322. sg_dma_address(sg) + offset, xfer_len);
  323. offset = 0;
  324. if (size == 0)
  325. break;
  326. }
  327. pxa_dma->sg_cpu[sglen].ddadr = DDADR_STOP;
  328. pxa_dma->sg_cpu[sglen].dcmd = DCMD_FLOWSRC | DCMD_BURST8 | DCMD_ENDIRQEN;
  329. /*
  330. * Handle 1 special case :
  331. * - in 3 planes (YUV422P format), we might finish with xfer_len equal
  332. * to dma_len (end on PAGE boundary). In this case, the sg element
  333. * for next plane should be the next after the last used to store the
  334. * last scatter gather RAM page
  335. */
  336. if (xfer_len >= dma_len) {
  337. *sg_first_ofs = xfer_len - dma_len;
  338. *sg_first = sg_next(sg);
  339. } else {
  340. *sg_first_ofs = xfer_len;
  341. *sg_first = sg;
  342. }
  343. return 0;
  344. }
  345. static void pxa_videobuf_set_actdma(struct pxa_camera_dev *pcdev,
  346. struct pxa_buffer *buf)
  347. {
  348. buf->active_dma = DMA_Y;
  349. if (pcdev->channels == 3)
  350. buf->active_dma |= DMA_U | DMA_V;
  351. }
  352. /*
  353. * Please check the DMA prepared buffer structure in :
  354. * Documentation/video4linux/pxa_camera.txt
  355. * Please check also in pxa_camera_check_link_miss() to understand why DMA chain
  356. * modification while DMA chain is running will work anyway.
  357. */
  358. static int pxa_videobuf_prepare(struct videobuf_queue *vq,
  359. struct videobuf_buffer *vb, enum v4l2_field field)
  360. {
  361. struct soc_camera_device *icd = vq->priv_data;
  362. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  363. struct pxa_camera_dev *pcdev = ici->priv;
  364. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  365. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  366. int ret;
  367. int size_y, size_u = 0, size_v = 0;
  368. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  369. vb, vb->baddr, vb->bsize);
  370. /* Added list head initialization on alloc */
  371. WARN_ON(!list_empty(&vb->queue));
  372. #ifdef DEBUG
  373. /*
  374. * This can be useful if you want to see if we actually fill
  375. * the buffer with something
  376. */
  377. memset((void *)vb->baddr, 0xaa, vb->bsize);
  378. #endif
  379. BUG_ON(NULL == icd->current_fmt);
  380. /*
  381. * I think, in buf_prepare you only have to protect global data,
  382. * the actual buffer is yours
  383. */
  384. buf->inwork = 1;
  385. if (buf->code != icd->current_fmt->code ||
  386. vb->width != icd->user_width ||
  387. vb->height != icd->user_height ||
  388. vb->field != field) {
  389. buf->code = icd->current_fmt->code;
  390. vb->width = icd->user_width;
  391. vb->height = icd->user_height;
  392. vb->field = field;
  393. vb->state = VIDEOBUF_NEEDS_INIT;
  394. }
  395. vb->size = icd->sizeimage;
  396. if (0 != vb->baddr && vb->bsize < vb->size) {
  397. ret = -EINVAL;
  398. goto out;
  399. }
  400. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  401. int size = vb->size;
  402. int next_ofs = 0;
  403. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  404. struct scatterlist *sg;
  405. ret = videobuf_iolock(vq, vb, NULL);
  406. if (ret)
  407. goto fail;
  408. if (pcdev->channels == 3) {
  409. size_y = size / 2;
  410. size_u = size_v = size / 4;
  411. } else {
  412. size_y = size;
  413. }
  414. sg = dma->sglist;
  415. /* init DMA for Y channel */
  416. ret = pxa_init_dma_channel(pcdev, buf, dma, 0, CIBR0, size_y,
  417. &sg, &next_ofs);
  418. if (ret) {
  419. dev_err(dev, "DMA initialization for Y/RGB failed\n");
  420. goto fail;
  421. }
  422. /* init DMA for U channel */
  423. if (size_u)
  424. ret = pxa_init_dma_channel(pcdev, buf, dma, 1, CIBR1,
  425. size_u, &sg, &next_ofs);
  426. if (ret) {
  427. dev_err(dev, "DMA initialization for U failed\n");
  428. goto fail_u;
  429. }
  430. /* init DMA for V channel */
  431. if (size_v)
  432. ret = pxa_init_dma_channel(pcdev, buf, dma, 2, CIBR2,
  433. size_v, &sg, &next_ofs);
  434. if (ret) {
  435. dev_err(dev, "DMA initialization for V failed\n");
  436. goto fail_v;
  437. }
  438. vb->state = VIDEOBUF_PREPARED;
  439. }
  440. buf->inwork = 0;
  441. pxa_videobuf_set_actdma(pcdev, buf);
  442. return 0;
  443. fail_v:
  444. dma_free_coherent(dev, buf->dmas[1].sg_size,
  445. buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
  446. fail_u:
  447. dma_free_coherent(dev, buf->dmas[0].sg_size,
  448. buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
  449. fail:
  450. free_buffer(vq, buf);
  451. out:
  452. buf->inwork = 0;
  453. return ret;
  454. }
  455. /**
  456. * pxa_dma_start_channels - start DMA channel for active buffer
  457. * @pcdev: pxa camera device
  458. *
  459. * Initialize DMA channels to the beginning of the active video buffer, and
  460. * start these channels.
  461. */
  462. static void pxa_dma_start_channels(struct pxa_camera_dev *pcdev)
  463. {
  464. int i;
  465. struct pxa_buffer *active;
  466. active = pcdev->active;
  467. for (i = 0; i < pcdev->channels; i++) {
  468. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  469. "%s (channel=%d) ddadr=%08x\n", __func__,
  470. i, active->dmas[i].sg_dma);
  471. DDADR(pcdev->dma_chans[i]) = active->dmas[i].sg_dma;
  472. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  473. }
  474. }
  475. static void pxa_dma_stop_channels(struct pxa_camera_dev *pcdev)
  476. {
  477. int i;
  478. for (i = 0; i < pcdev->channels; i++) {
  479. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  480. "%s (channel=%d)\n", __func__, i);
  481. DCSR(pcdev->dma_chans[i]) = 0;
  482. }
  483. }
  484. static void pxa_dma_add_tail_buf(struct pxa_camera_dev *pcdev,
  485. struct pxa_buffer *buf)
  486. {
  487. int i;
  488. struct pxa_dma_desc *buf_last_desc;
  489. for (i = 0; i < pcdev->channels; i++) {
  490. buf_last_desc = buf->dmas[i].sg_cpu + buf->dmas[i].sglen;
  491. buf_last_desc->ddadr = DDADR_STOP;
  492. if (pcdev->sg_tail[i])
  493. /* Link the new buffer to the old tail */
  494. pcdev->sg_tail[i]->ddadr = buf->dmas[i].sg_dma;
  495. /* Update the channel tail */
  496. pcdev->sg_tail[i] = buf_last_desc;
  497. }
  498. }
  499. /**
  500. * pxa_camera_start_capture - start video capturing
  501. * @pcdev: camera device
  502. *
  503. * Launch capturing. DMA channels should not be active yet. They should get
  504. * activated at the end of frame interrupt, to capture only whole frames, and
  505. * never begin the capture of a partial frame.
  506. */
  507. static void pxa_camera_start_capture(struct pxa_camera_dev *pcdev)
  508. {
  509. unsigned long cicr0;
  510. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
  511. /* Enable End-Of-Frame Interrupt */
  512. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_ENB;
  513. cicr0 &= ~CICR0_EOFM;
  514. __raw_writel(cicr0, pcdev->base + CICR0);
  515. }
  516. static void pxa_camera_stop_capture(struct pxa_camera_dev *pcdev)
  517. {
  518. unsigned long cicr0;
  519. pxa_dma_stop_channels(pcdev);
  520. cicr0 = __raw_readl(pcdev->base + CICR0) & ~CICR0_ENB;
  521. __raw_writel(cicr0, pcdev->base + CICR0);
  522. pcdev->active = NULL;
  523. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s\n", __func__);
  524. }
  525. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  526. static void pxa_videobuf_queue(struct videobuf_queue *vq,
  527. struct videobuf_buffer *vb)
  528. {
  529. struct soc_camera_device *icd = vq->priv_data;
  530. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  531. struct pxa_camera_dev *pcdev = ici->priv;
  532. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  533. dev_dbg(icd->parent, "%s (vb=0x%p) 0x%08lx %d active=%p\n",
  534. __func__, vb, vb->baddr, vb->bsize, pcdev->active);
  535. list_add_tail(&vb->queue, &pcdev->capture);
  536. vb->state = VIDEOBUF_ACTIVE;
  537. pxa_dma_add_tail_buf(pcdev, buf);
  538. if (!pcdev->active)
  539. pxa_camera_start_capture(pcdev);
  540. }
  541. static void pxa_videobuf_release(struct videobuf_queue *vq,
  542. struct videobuf_buffer *vb)
  543. {
  544. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  545. #ifdef DEBUG
  546. struct soc_camera_device *icd = vq->priv_data;
  547. struct device *dev = icd->parent;
  548. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  549. vb, vb->baddr, vb->bsize);
  550. switch (vb->state) {
  551. case VIDEOBUF_ACTIVE:
  552. dev_dbg(dev, "%s (active)\n", __func__);
  553. break;
  554. case VIDEOBUF_QUEUED:
  555. dev_dbg(dev, "%s (queued)\n", __func__);
  556. break;
  557. case VIDEOBUF_PREPARED:
  558. dev_dbg(dev, "%s (prepared)\n", __func__);
  559. break;
  560. default:
  561. dev_dbg(dev, "%s (unknown)\n", __func__);
  562. break;
  563. }
  564. #endif
  565. free_buffer(vq, buf);
  566. }
  567. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  568. struct videobuf_buffer *vb,
  569. struct pxa_buffer *buf)
  570. {
  571. int i;
  572. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  573. list_del_init(&vb->queue);
  574. vb->state = VIDEOBUF_DONE;
  575. v4l2_get_timestamp(&vb->ts);
  576. vb->field_count++;
  577. wake_up(&vb->done);
  578. dev_dbg(pcdev->soc_host.v4l2_dev.dev, "%s dequeud buffer (vb=0x%p)\n",
  579. __func__, vb);
  580. if (list_empty(&pcdev->capture)) {
  581. pxa_camera_stop_capture(pcdev);
  582. for (i = 0; i < pcdev->channels; i++)
  583. pcdev->sg_tail[i] = NULL;
  584. return;
  585. }
  586. pcdev->active = list_entry(pcdev->capture.next,
  587. struct pxa_buffer, vb.queue);
  588. }
  589. /**
  590. * pxa_camera_check_link_miss - check missed DMA linking
  591. * @pcdev: camera device
  592. *
  593. * The DMA chaining is done with DMA running. This means a tiny temporal window
  594. * remains, where a buffer is queued on the chain, while the chain is already
  595. * stopped. This means the tailed buffer would never be transferred by DMA.
  596. * This function restarts the capture for this corner case, where :
  597. * - DADR() == DADDR_STOP
  598. * - a videobuffer is queued on the pcdev->capture list
  599. *
  600. * Please check the "DMA hot chaining timeslice issue" in
  601. * Documentation/video4linux/pxa_camera.txt
  602. *
  603. * Context: should only be called within the dma irq handler
  604. */
  605. static void pxa_camera_check_link_miss(struct pxa_camera_dev *pcdev)
  606. {
  607. int i, is_dma_stopped = 1;
  608. for (i = 0; i < pcdev->channels; i++)
  609. if (DDADR(pcdev->dma_chans[i]) != DDADR_STOP)
  610. is_dma_stopped = 0;
  611. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  612. "%s : top queued buffer=%p, dma_stopped=%d\n",
  613. __func__, pcdev->active, is_dma_stopped);
  614. if (pcdev->active && is_dma_stopped)
  615. pxa_camera_start_capture(pcdev);
  616. }
  617. static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
  618. enum pxa_camera_active_dma act_dma)
  619. {
  620. struct device *dev = pcdev->soc_host.v4l2_dev.dev;
  621. struct pxa_buffer *buf;
  622. unsigned long flags;
  623. u32 status, camera_status, overrun;
  624. struct videobuf_buffer *vb;
  625. spin_lock_irqsave(&pcdev->lock, flags);
  626. status = DCSR(channel);
  627. DCSR(channel) = status;
  628. camera_status = __raw_readl(pcdev->base + CISR);
  629. overrun = CISR_IFO_0;
  630. if (pcdev->channels == 3)
  631. overrun |= CISR_IFO_1 | CISR_IFO_2;
  632. if (status & DCSR_BUSERR) {
  633. dev_err(dev, "DMA Bus Error IRQ!\n");
  634. goto out;
  635. }
  636. if (!(status & (DCSR_ENDINTR | DCSR_STARTINTR))) {
  637. dev_err(dev, "Unknown DMA IRQ source, status: 0x%08x\n",
  638. status);
  639. goto out;
  640. }
  641. /*
  642. * pcdev->active should not be NULL in DMA irq handler.
  643. *
  644. * But there is one corner case : if capture was stopped due to an
  645. * overrun of channel 1, and at that same channel 2 was completed.
  646. *
  647. * When handling the overrun in DMA irq for channel 1, we'll stop the
  648. * capture and restart it (and thus set pcdev->active to NULL). But the
  649. * DMA irq handler will already be pending for channel 2. So on entering
  650. * the DMA irq handler for channel 2 there will be no active buffer, yet
  651. * that is normal.
  652. */
  653. if (!pcdev->active)
  654. goto out;
  655. vb = &pcdev->active->vb;
  656. buf = container_of(vb, struct pxa_buffer, vb);
  657. WARN_ON(buf->inwork || list_empty(&vb->queue));
  658. dev_dbg(dev, "%s channel=%d %s%s(vb=0x%p) dma.desc=%x\n",
  659. __func__, channel, status & DCSR_STARTINTR ? "SOF " : "",
  660. status & DCSR_ENDINTR ? "EOF " : "", vb, DDADR(channel));
  661. if (status & DCSR_ENDINTR) {
  662. /*
  663. * It's normal if the last frame creates an overrun, as there
  664. * are no more DMA descriptors to fetch from QCI fifos
  665. */
  666. if (camera_status & overrun &&
  667. !list_is_last(pcdev->capture.next, &pcdev->capture)) {
  668. dev_dbg(dev, "FIFO overrun! CISR: %x\n",
  669. camera_status);
  670. pxa_camera_stop_capture(pcdev);
  671. pxa_camera_start_capture(pcdev);
  672. goto out;
  673. }
  674. buf->active_dma &= ~act_dma;
  675. if (!buf->active_dma) {
  676. pxa_camera_wakeup(pcdev, vb, buf);
  677. pxa_camera_check_link_miss(pcdev);
  678. }
  679. }
  680. out:
  681. spin_unlock_irqrestore(&pcdev->lock, flags);
  682. }
  683. static void pxa_camera_dma_irq_y(int channel, void *data)
  684. {
  685. struct pxa_camera_dev *pcdev = data;
  686. pxa_camera_dma_irq(channel, pcdev, DMA_Y);
  687. }
  688. static void pxa_camera_dma_irq_u(int channel, void *data)
  689. {
  690. struct pxa_camera_dev *pcdev = data;
  691. pxa_camera_dma_irq(channel, pcdev, DMA_U);
  692. }
  693. static void pxa_camera_dma_irq_v(int channel, void *data)
  694. {
  695. struct pxa_camera_dev *pcdev = data;
  696. pxa_camera_dma_irq(channel, pcdev, DMA_V);
  697. }
  698. static struct videobuf_queue_ops pxa_videobuf_ops = {
  699. .buf_setup = pxa_videobuf_setup,
  700. .buf_prepare = pxa_videobuf_prepare,
  701. .buf_queue = pxa_videobuf_queue,
  702. .buf_release = pxa_videobuf_release,
  703. };
  704. static void pxa_camera_init_videobuf(struct videobuf_queue *q,
  705. struct soc_camera_device *icd)
  706. {
  707. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  708. struct pxa_camera_dev *pcdev = ici->priv;
  709. /*
  710. * We must pass NULL as dev pointer, then all pci_* dma operations
  711. * transform to normal dma_* ones.
  712. */
  713. videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
  714. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  715. sizeof(struct pxa_buffer), icd, &ici->host_lock);
  716. }
  717. static u32 mclk_get_divisor(struct platform_device *pdev,
  718. struct pxa_camera_dev *pcdev)
  719. {
  720. unsigned long mclk = pcdev->mclk;
  721. struct device *dev = &pdev->dev;
  722. u32 div;
  723. unsigned long lcdclk;
  724. lcdclk = clk_get_rate(pcdev->clk);
  725. pcdev->ciclk = lcdclk;
  726. /* mclk <= ciclk / 4 (27.4.2) */
  727. if (mclk > lcdclk / 4) {
  728. mclk = lcdclk / 4;
  729. dev_warn(dev, "Limiting master clock to %lu\n", mclk);
  730. }
  731. /* We verify mclk != 0, so if anyone breaks it, here comes their Oops */
  732. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  733. /* If we're not supplying MCLK, leave it at 0 */
  734. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  735. pcdev->mclk = lcdclk / (2 * (div + 1));
  736. dev_dbg(dev, "LCD clock %luHz, target freq %luHz, divisor %u\n",
  737. lcdclk, mclk, div);
  738. return div;
  739. }
  740. static void recalculate_fifo_timeout(struct pxa_camera_dev *pcdev,
  741. unsigned long pclk)
  742. {
  743. /* We want a timeout > 1 pixel time, not ">=" */
  744. u32 ciclk_per_pixel = pcdev->ciclk / pclk + 1;
  745. __raw_writel(ciclk_per_pixel, pcdev->base + CITOR);
  746. }
  747. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  748. {
  749. u32 cicr4 = 0;
  750. /* disable all interrupts */
  751. __raw_writel(0x3ff, pcdev->base + CICR0);
  752. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  753. cicr4 |= CICR4_PCLK_EN;
  754. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  755. cicr4 |= CICR4_MCLK_EN;
  756. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  757. cicr4 |= CICR4_PCP;
  758. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  759. cicr4 |= CICR4_HSP;
  760. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  761. cicr4 |= CICR4_VSP;
  762. __raw_writel(pcdev->mclk_divisor | cicr4, pcdev->base + CICR4);
  763. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  764. /* Initialise the timeout under the assumption pclk = mclk */
  765. recalculate_fifo_timeout(pcdev, pcdev->mclk);
  766. else
  767. /* "Safe default" - 13MHz */
  768. recalculate_fifo_timeout(pcdev, 13000000);
  769. clk_prepare_enable(pcdev->clk);
  770. }
  771. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  772. {
  773. clk_disable_unprepare(pcdev->clk);
  774. }
  775. static irqreturn_t pxa_camera_irq(int irq, void *data)
  776. {
  777. struct pxa_camera_dev *pcdev = data;
  778. unsigned long status, cifr, cicr0;
  779. struct pxa_buffer *buf;
  780. struct videobuf_buffer *vb;
  781. status = __raw_readl(pcdev->base + CISR);
  782. dev_dbg(pcdev->soc_host.v4l2_dev.dev,
  783. "Camera interrupt status 0x%lx\n", status);
  784. if (!status)
  785. return IRQ_NONE;
  786. __raw_writel(status, pcdev->base + CISR);
  787. if (status & CISR_EOF) {
  788. /* Reset the FIFOs */
  789. cifr = __raw_readl(pcdev->base + CIFR) | CIFR_RESET_F;
  790. __raw_writel(cifr, pcdev->base + CIFR);
  791. pcdev->active = list_first_entry(&pcdev->capture,
  792. struct pxa_buffer, vb.queue);
  793. vb = &pcdev->active->vb;
  794. buf = container_of(vb, struct pxa_buffer, vb);
  795. pxa_videobuf_set_actdma(pcdev, buf);
  796. pxa_dma_start_channels(pcdev);
  797. cicr0 = __raw_readl(pcdev->base + CICR0) | CICR0_EOFM;
  798. __raw_writel(cicr0, pcdev->base + CICR0);
  799. }
  800. return IRQ_HANDLED;
  801. }
  802. static int pxa_camera_add_device(struct soc_camera_device *icd)
  803. {
  804. dev_info(icd->parent, "PXA Camera driver attached to camera %d\n",
  805. icd->devnum);
  806. return 0;
  807. }
  808. static void pxa_camera_remove_device(struct soc_camera_device *icd)
  809. {
  810. dev_info(icd->parent, "PXA Camera driver detached from camera %d\n",
  811. icd->devnum);
  812. }
  813. /*
  814. * The following two functions absolutely depend on the fact, that
  815. * there can be only one camera on PXA quick capture interface
  816. * Called with .host_lock held
  817. */
  818. static int pxa_camera_clock_start(struct soc_camera_host *ici)
  819. {
  820. struct pxa_camera_dev *pcdev = ici->priv;
  821. pxa_camera_activate(pcdev);
  822. return 0;
  823. }
  824. /* Called with .host_lock held */
  825. static void pxa_camera_clock_stop(struct soc_camera_host *ici)
  826. {
  827. struct pxa_camera_dev *pcdev = ici->priv;
  828. /* disable capture, disable interrupts */
  829. __raw_writel(0x3ff, pcdev->base + CICR0);
  830. /* Stop DMA engine */
  831. DCSR(pcdev->dma_chans[0]) = 0;
  832. DCSR(pcdev->dma_chans[1]) = 0;
  833. DCSR(pcdev->dma_chans[2]) = 0;
  834. pxa_camera_deactivate(pcdev);
  835. }
  836. static int test_platform_param(struct pxa_camera_dev *pcdev,
  837. unsigned char buswidth, unsigned long *flags)
  838. {
  839. /*
  840. * Platform specified synchronization and pixel clock polarities are
  841. * only a recommendation and are only used during probing. The PXA270
  842. * quick capture interface supports both.
  843. */
  844. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  845. V4L2_MBUS_MASTER : V4L2_MBUS_SLAVE) |
  846. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  847. V4L2_MBUS_HSYNC_ACTIVE_LOW |
  848. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  849. V4L2_MBUS_VSYNC_ACTIVE_LOW |
  850. V4L2_MBUS_DATA_ACTIVE_HIGH |
  851. V4L2_MBUS_PCLK_SAMPLE_RISING |
  852. V4L2_MBUS_PCLK_SAMPLE_FALLING;
  853. /* If requested data width is supported by the platform, use it */
  854. if ((1 << (buswidth - 1)) & pcdev->width_flags)
  855. return 0;
  856. return -EINVAL;
  857. }
  858. static void pxa_camera_setup_cicr(struct soc_camera_device *icd,
  859. unsigned long flags, __u32 pixfmt)
  860. {
  861. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  862. struct pxa_camera_dev *pcdev = ici->priv;
  863. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  864. unsigned long dw, bpp;
  865. u32 cicr0, cicr1, cicr2, cicr3, cicr4 = 0, y_skip_top;
  866. int ret = v4l2_subdev_call(sd, sensor, g_skip_top_lines, &y_skip_top);
  867. if (ret < 0)
  868. y_skip_top = 0;
  869. /*
  870. * Datawidth is now guaranteed to be equal to one of the three values.
  871. * We fix bit-per-pixel equal to data-width...
  872. */
  873. switch (icd->current_fmt->host_fmt->bits_per_sample) {
  874. case 10:
  875. dw = 4;
  876. bpp = 0x40;
  877. break;
  878. case 9:
  879. dw = 3;
  880. bpp = 0x20;
  881. break;
  882. default:
  883. /*
  884. * Actually it can only be 8 now,
  885. * default is just to silence compiler warnings
  886. */
  887. case 8:
  888. dw = 2;
  889. bpp = 0;
  890. }
  891. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  892. cicr4 |= CICR4_PCLK_EN;
  893. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  894. cicr4 |= CICR4_MCLK_EN;
  895. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  896. cicr4 |= CICR4_PCP;
  897. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  898. cicr4 |= CICR4_HSP;
  899. if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  900. cicr4 |= CICR4_VSP;
  901. cicr0 = __raw_readl(pcdev->base + CICR0);
  902. if (cicr0 & CICR0_ENB)
  903. __raw_writel(cicr0 & ~CICR0_ENB, pcdev->base + CICR0);
  904. cicr1 = CICR1_PPL_VAL(icd->user_width - 1) | bpp | dw;
  905. switch (pixfmt) {
  906. case V4L2_PIX_FMT_YUV422P:
  907. pcdev->channels = 3;
  908. cicr1 |= CICR1_YCBCR_F;
  909. /*
  910. * Normally, pxa bus wants as input UYVY format. We allow all
  911. * reorderings of the YUV422 format, as no processing is done,
  912. * and the YUV stream is just passed through without any
  913. * transformation. Note that UYVY is the only format that
  914. * should be used if pxa framebuffer Overlay2 is used.
  915. */
  916. case V4L2_PIX_FMT_UYVY:
  917. case V4L2_PIX_FMT_VYUY:
  918. case V4L2_PIX_FMT_YUYV:
  919. case V4L2_PIX_FMT_YVYU:
  920. cicr1 |= CICR1_COLOR_SP_VAL(2);
  921. break;
  922. case V4L2_PIX_FMT_RGB555:
  923. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  924. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  925. break;
  926. case V4L2_PIX_FMT_RGB565:
  927. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  928. break;
  929. }
  930. cicr2 = 0;
  931. cicr3 = CICR3_LPF_VAL(icd->user_height - 1) |
  932. CICR3_BFW_VAL(min((u32)255, y_skip_top));
  933. cicr4 |= pcdev->mclk_divisor;
  934. __raw_writel(cicr1, pcdev->base + CICR1);
  935. __raw_writel(cicr2, pcdev->base + CICR2);
  936. __raw_writel(cicr3, pcdev->base + CICR3);
  937. __raw_writel(cicr4, pcdev->base + CICR4);
  938. /* CIF interrupts are not used, only DMA */
  939. cicr0 = (cicr0 & CICR0_ENB) | (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  940. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP));
  941. cicr0 |= CICR0_DMAEN | CICR0_IRQ_MASK;
  942. __raw_writel(cicr0, pcdev->base + CICR0);
  943. }
  944. static int pxa_camera_set_bus_param(struct soc_camera_device *icd)
  945. {
  946. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  947. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  948. struct pxa_camera_dev *pcdev = ici->priv;
  949. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  950. u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
  951. unsigned long bus_flags, common_flags;
  952. int ret;
  953. struct pxa_cam *cam = icd->host_priv;
  954. ret = test_platform_param(pcdev, icd->current_fmt->host_fmt->bits_per_sample,
  955. &bus_flags);
  956. if (ret < 0)
  957. return ret;
  958. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  959. if (!ret) {
  960. common_flags = soc_mbus_config_compatible(&cfg,
  961. bus_flags);
  962. if (!common_flags) {
  963. dev_warn(icd->parent,
  964. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  965. cfg.flags, bus_flags);
  966. return -EINVAL;
  967. }
  968. } else if (ret != -ENOIOCTLCMD) {
  969. return ret;
  970. } else {
  971. common_flags = bus_flags;
  972. }
  973. pcdev->channels = 1;
  974. /* Make choises, based on platform preferences */
  975. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  976. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  977. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  978. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  979. else
  980. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  981. }
  982. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  983. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  984. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  985. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  986. else
  987. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  988. }
  989. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  990. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  991. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  992. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  993. else
  994. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  995. }
  996. cfg.flags = common_flags;
  997. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  998. if (ret < 0 && ret != -ENOIOCTLCMD) {
  999. dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
  1000. common_flags, ret);
  1001. return ret;
  1002. }
  1003. cam->flags = common_flags;
  1004. pxa_camera_setup_cicr(icd, common_flags, pixfmt);
  1005. return 0;
  1006. }
  1007. static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
  1008. unsigned char buswidth)
  1009. {
  1010. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1011. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1012. struct pxa_camera_dev *pcdev = ici->priv;
  1013. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1014. unsigned long bus_flags, common_flags;
  1015. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  1016. if (ret < 0)
  1017. return ret;
  1018. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  1019. if (!ret) {
  1020. common_flags = soc_mbus_config_compatible(&cfg,
  1021. bus_flags);
  1022. if (!common_flags) {
  1023. dev_warn(icd->parent,
  1024. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  1025. cfg.flags, bus_flags);
  1026. return -EINVAL;
  1027. }
  1028. } else if (ret == -ENOIOCTLCMD) {
  1029. ret = 0;
  1030. }
  1031. return ret;
  1032. }
  1033. static const struct soc_mbus_pixelfmt pxa_camera_formats[] = {
  1034. {
  1035. .fourcc = V4L2_PIX_FMT_YUV422P,
  1036. .name = "Planar YUV422 16 bit",
  1037. .bits_per_sample = 8,
  1038. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  1039. .order = SOC_MBUS_ORDER_LE,
  1040. .layout = SOC_MBUS_LAYOUT_PLANAR_2Y_U_V,
  1041. },
  1042. };
  1043. /* This will be corrected as we get more formats */
  1044. static bool pxa_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  1045. {
  1046. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  1047. (fmt->bits_per_sample == 8 &&
  1048. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  1049. (fmt->bits_per_sample > 8 &&
  1050. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  1051. }
  1052. static int pxa_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
  1053. struct soc_camera_format_xlate *xlate)
  1054. {
  1055. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1056. struct device *dev = icd->parent;
  1057. int formats = 0, ret;
  1058. struct pxa_cam *cam;
  1059. struct v4l2_subdev_mbus_code_enum code = {
  1060. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1061. .index = idx,
  1062. };
  1063. const struct soc_mbus_pixelfmt *fmt;
  1064. ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
  1065. if (ret < 0)
  1066. /* No more formats */
  1067. return 0;
  1068. fmt = soc_mbus_get_fmtdesc(code.code);
  1069. if (!fmt) {
  1070. dev_err(dev, "Invalid format code #%u: %d\n", idx, code.code);
  1071. return 0;
  1072. }
  1073. /* This also checks support for the requested bits-per-sample */
  1074. ret = pxa_camera_try_bus_param(icd, fmt->bits_per_sample);
  1075. if (ret < 0)
  1076. return 0;
  1077. if (!icd->host_priv) {
  1078. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  1079. if (!cam)
  1080. return -ENOMEM;
  1081. icd->host_priv = cam;
  1082. } else {
  1083. cam = icd->host_priv;
  1084. }
  1085. switch (code.code) {
  1086. case MEDIA_BUS_FMT_UYVY8_2X8:
  1087. formats++;
  1088. if (xlate) {
  1089. xlate->host_fmt = &pxa_camera_formats[0];
  1090. xlate->code = code.code;
  1091. xlate++;
  1092. dev_dbg(dev, "Providing format %s using code %d\n",
  1093. pxa_camera_formats[0].name, code.code);
  1094. }
  1095. case MEDIA_BUS_FMT_VYUY8_2X8:
  1096. case MEDIA_BUS_FMT_YUYV8_2X8:
  1097. case MEDIA_BUS_FMT_YVYU8_2X8:
  1098. case MEDIA_BUS_FMT_RGB565_2X8_LE:
  1099. case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
  1100. if (xlate)
  1101. dev_dbg(dev, "Providing format %s packed\n",
  1102. fmt->name);
  1103. break;
  1104. default:
  1105. if (!pxa_camera_packing_supported(fmt))
  1106. return 0;
  1107. if (xlate)
  1108. dev_dbg(dev,
  1109. "Providing format %s in pass-through mode\n",
  1110. fmt->name);
  1111. }
  1112. /* Generic pass-through */
  1113. formats++;
  1114. if (xlate) {
  1115. xlate->host_fmt = fmt;
  1116. xlate->code = code.code;
  1117. xlate++;
  1118. }
  1119. return formats;
  1120. }
  1121. static void pxa_camera_put_formats(struct soc_camera_device *icd)
  1122. {
  1123. kfree(icd->host_priv);
  1124. icd->host_priv = NULL;
  1125. }
  1126. static int pxa_camera_check_frame(u32 width, u32 height)
  1127. {
  1128. /* limit to pxa hardware capabilities */
  1129. return height < 32 || height > 2048 || width < 48 || width > 2048 ||
  1130. (width & 0x01);
  1131. }
  1132. static int pxa_camera_set_crop(struct soc_camera_device *icd,
  1133. const struct v4l2_crop *a)
  1134. {
  1135. const struct v4l2_rect *rect = &a->c;
  1136. struct device *dev = icd->parent;
  1137. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1138. struct pxa_camera_dev *pcdev = ici->priv;
  1139. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1140. struct soc_camera_sense sense = {
  1141. .master_clock = pcdev->mclk,
  1142. .pixel_clock_max = pcdev->ciclk / 4,
  1143. };
  1144. struct v4l2_subdev_format fmt = {
  1145. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1146. };
  1147. struct v4l2_mbus_framefmt *mf = &fmt.format;
  1148. struct pxa_cam *cam = icd->host_priv;
  1149. u32 fourcc = icd->current_fmt->host_fmt->fourcc;
  1150. int ret;
  1151. /* If PCLK is used to latch data from the sensor, check sense */
  1152. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1153. icd->sense = &sense;
  1154. ret = v4l2_subdev_call(sd, video, s_crop, a);
  1155. icd->sense = NULL;
  1156. if (ret < 0) {
  1157. dev_warn(dev, "Failed to crop to %ux%u@%u:%u\n",
  1158. rect->width, rect->height, rect->left, rect->top);
  1159. return ret;
  1160. }
  1161. ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
  1162. if (ret < 0)
  1163. return ret;
  1164. if (pxa_camera_check_frame(mf->width, mf->height)) {
  1165. /*
  1166. * Camera cropping produced a frame beyond our capabilities.
  1167. * FIXME: just extract a subframe, that we can process.
  1168. */
  1169. v4l_bound_align_image(&mf->width, 48, 2048, 1,
  1170. &mf->height, 32, 2048, 0,
  1171. fourcc == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1172. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &fmt);
  1173. if (ret < 0)
  1174. return ret;
  1175. if (pxa_camera_check_frame(mf->width, mf->height)) {
  1176. dev_warn(icd->parent,
  1177. "Inconsistent state. Use S_FMT to repair\n");
  1178. return -EINVAL;
  1179. }
  1180. }
  1181. if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
  1182. if (sense.pixel_clock > sense.pixel_clock_max) {
  1183. dev_err(dev,
  1184. "pixel clock %lu set by the camera too high!",
  1185. sense.pixel_clock);
  1186. return -EIO;
  1187. }
  1188. recalculate_fifo_timeout(pcdev, sense.pixel_clock);
  1189. }
  1190. icd->user_width = mf->width;
  1191. icd->user_height = mf->height;
  1192. pxa_camera_setup_cicr(icd, cam->flags, fourcc);
  1193. return ret;
  1194. }
  1195. static int pxa_camera_set_fmt(struct soc_camera_device *icd,
  1196. struct v4l2_format *f)
  1197. {
  1198. struct device *dev = icd->parent;
  1199. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1200. struct pxa_camera_dev *pcdev = ici->priv;
  1201. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1202. const struct soc_camera_format_xlate *xlate = NULL;
  1203. struct soc_camera_sense sense = {
  1204. .master_clock = pcdev->mclk,
  1205. .pixel_clock_max = pcdev->ciclk / 4,
  1206. };
  1207. struct v4l2_pix_format *pix = &f->fmt.pix;
  1208. struct v4l2_subdev_format format = {
  1209. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  1210. };
  1211. struct v4l2_mbus_framefmt *mf = &format.format;
  1212. int ret;
  1213. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1214. if (!xlate) {
  1215. dev_warn(dev, "Format %x not found\n", pix->pixelformat);
  1216. return -EINVAL;
  1217. }
  1218. /* If PCLK is used to latch data from the sensor, check sense */
  1219. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  1220. /* The caller holds a mutex. */
  1221. icd->sense = &sense;
  1222. mf->width = pix->width;
  1223. mf->height = pix->height;
  1224. mf->field = pix->field;
  1225. mf->colorspace = pix->colorspace;
  1226. mf->code = xlate->code;
  1227. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format);
  1228. if (mf->code != xlate->code)
  1229. return -EINVAL;
  1230. icd->sense = NULL;
  1231. if (ret < 0) {
  1232. dev_warn(dev, "Failed to configure for format %x\n",
  1233. pix->pixelformat);
  1234. } else if (pxa_camera_check_frame(mf->width, mf->height)) {
  1235. dev_warn(dev,
  1236. "Camera driver produced an unsupported frame %dx%d\n",
  1237. mf->width, mf->height);
  1238. ret = -EINVAL;
  1239. } else if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) {
  1240. if (sense.pixel_clock > sense.pixel_clock_max) {
  1241. dev_err(dev,
  1242. "pixel clock %lu set by the camera too high!",
  1243. sense.pixel_clock);
  1244. return -EIO;
  1245. }
  1246. recalculate_fifo_timeout(pcdev, sense.pixel_clock);
  1247. }
  1248. if (ret < 0)
  1249. return ret;
  1250. pix->width = mf->width;
  1251. pix->height = mf->height;
  1252. pix->field = mf->field;
  1253. pix->colorspace = mf->colorspace;
  1254. icd->current_fmt = xlate;
  1255. return ret;
  1256. }
  1257. static int pxa_camera_try_fmt(struct soc_camera_device *icd,
  1258. struct v4l2_format *f)
  1259. {
  1260. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1261. const struct soc_camera_format_xlate *xlate;
  1262. struct v4l2_pix_format *pix = &f->fmt.pix;
  1263. struct v4l2_subdev_pad_config pad_cfg;
  1264. struct v4l2_subdev_format format = {
  1265. .which = V4L2_SUBDEV_FORMAT_TRY,
  1266. };
  1267. struct v4l2_mbus_framefmt *mf = &format.format;
  1268. __u32 pixfmt = pix->pixelformat;
  1269. int ret;
  1270. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1271. if (!xlate) {
  1272. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  1273. return -EINVAL;
  1274. }
  1275. /*
  1276. * Limit to pxa hardware capabilities. YUV422P planar format requires
  1277. * images size to be a multiple of 16 bytes. If not, zeros will be
  1278. * inserted between Y and U planes, and U and V planes, which violates
  1279. * the YUV422P standard.
  1280. */
  1281. v4l_bound_align_image(&pix->width, 48, 2048, 1,
  1282. &pix->height, 32, 2048, 0,
  1283. pixfmt == V4L2_PIX_FMT_YUV422P ? 4 : 0);
  1284. /* limit to sensor capabilities */
  1285. mf->width = pix->width;
  1286. mf->height = pix->height;
  1287. /* Only progressive video supported so far */
  1288. mf->field = V4L2_FIELD_NONE;
  1289. mf->colorspace = pix->colorspace;
  1290. mf->code = xlate->code;
  1291. ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
  1292. if (ret < 0)
  1293. return ret;
  1294. pix->width = mf->width;
  1295. pix->height = mf->height;
  1296. pix->colorspace = mf->colorspace;
  1297. switch (mf->field) {
  1298. case V4L2_FIELD_ANY:
  1299. case V4L2_FIELD_NONE:
  1300. pix->field = V4L2_FIELD_NONE;
  1301. break;
  1302. default:
  1303. /* TODO: support interlaced at least in pass-through mode */
  1304. dev_err(icd->parent, "Field type %d unsupported.\n",
  1305. mf->field);
  1306. return -EINVAL;
  1307. }
  1308. return ret;
  1309. }
  1310. static int pxa_camera_reqbufs(struct soc_camera_device *icd,
  1311. struct v4l2_requestbuffers *p)
  1312. {
  1313. int i;
  1314. /*
  1315. * This is for locking debugging only. I removed spinlocks and now I
  1316. * check whether .prepare is ever called on a linked buffer, or whether
  1317. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1318. * it hadn't triggered
  1319. */
  1320. for (i = 0; i < p->count; i++) {
  1321. struct pxa_buffer *buf = container_of(icd->vb_vidq.bufs[i],
  1322. struct pxa_buffer, vb);
  1323. buf->inwork = 0;
  1324. INIT_LIST_HEAD(&buf->vb.queue);
  1325. }
  1326. return 0;
  1327. }
  1328. static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
  1329. {
  1330. struct soc_camera_device *icd = file->private_data;
  1331. struct pxa_buffer *buf;
  1332. buf = list_entry(icd->vb_vidq.stream.next, struct pxa_buffer,
  1333. vb.stream);
  1334. poll_wait(file, &buf->vb.done, pt);
  1335. if (buf->vb.state == VIDEOBUF_DONE ||
  1336. buf->vb.state == VIDEOBUF_ERROR)
  1337. return POLLIN|POLLRDNORM;
  1338. return 0;
  1339. }
  1340. static int pxa_camera_querycap(struct soc_camera_host *ici,
  1341. struct v4l2_capability *cap)
  1342. {
  1343. /* cap->name is set by the firendly caller:-> */
  1344. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  1345. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1346. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1347. return 0;
  1348. }
  1349. static int pxa_camera_suspend(struct device *dev)
  1350. {
  1351. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1352. struct pxa_camera_dev *pcdev = ici->priv;
  1353. int i = 0, ret = 0;
  1354. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR0);
  1355. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR1);
  1356. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR2);
  1357. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR3);
  1358. pcdev->save_cicr[i++] = __raw_readl(pcdev->base + CICR4);
  1359. if (pcdev->soc_host.icd) {
  1360. struct v4l2_subdev *sd = soc_camera_to_subdev(pcdev->soc_host.icd);
  1361. ret = v4l2_subdev_call(sd, core, s_power, 0);
  1362. if (ret == -ENOIOCTLCMD)
  1363. ret = 0;
  1364. }
  1365. return ret;
  1366. }
  1367. static int pxa_camera_resume(struct device *dev)
  1368. {
  1369. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1370. struct pxa_camera_dev *pcdev = ici->priv;
  1371. int i = 0, ret = 0;
  1372. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1373. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1374. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1375. __raw_writel(pcdev->save_cicr[i++] & ~CICR0_ENB, pcdev->base + CICR0);
  1376. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR1);
  1377. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR2);
  1378. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR3);
  1379. __raw_writel(pcdev->save_cicr[i++], pcdev->base + CICR4);
  1380. if (pcdev->soc_host.icd) {
  1381. struct v4l2_subdev *sd = soc_camera_to_subdev(pcdev->soc_host.icd);
  1382. ret = v4l2_subdev_call(sd, core, s_power, 1);
  1383. if (ret == -ENOIOCTLCMD)
  1384. ret = 0;
  1385. }
  1386. /* Restart frame capture if active buffer exists */
  1387. if (!ret && pcdev->active)
  1388. pxa_camera_start_capture(pcdev);
  1389. return ret;
  1390. }
  1391. static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
  1392. .owner = THIS_MODULE,
  1393. .add = pxa_camera_add_device,
  1394. .remove = pxa_camera_remove_device,
  1395. .clock_start = pxa_camera_clock_start,
  1396. .clock_stop = pxa_camera_clock_stop,
  1397. .set_crop = pxa_camera_set_crop,
  1398. .get_formats = pxa_camera_get_formats,
  1399. .put_formats = pxa_camera_put_formats,
  1400. .set_fmt = pxa_camera_set_fmt,
  1401. .try_fmt = pxa_camera_try_fmt,
  1402. .init_videobuf = pxa_camera_init_videobuf,
  1403. .reqbufs = pxa_camera_reqbufs,
  1404. .poll = pxa_camera_poll,
  1405. .querycap = pxa_camera_querycap,
  1406. .set_bus_param = pxa_camera_set_bus_param,
  1407. };
  1408. static int pxa_camera_pdata_from_dt(struct device *dev,
  1409. struct pxa_camera_dev *pcdev)
  1410. {
  1411. u32 mclk_rate;
  1412. struct device_node *np = dev->of_node;
  1413. struct v4l2_of_endpoint ep;
  1414. int err = of_property_read_u32(np, "clock-frequency",
  1415. &mclk_rate);
  1416. if (!err) {
  1417. pcdev->platform_flags |= PXA_CAMERA_MCLK_EN;
  1418. pcdev->mclk = mclk_rate;
  1419. }
  1420. np = of_graph_get_next_endpoint(np, NULL);
  1421. if (!np) {
  1422. dev_err(dev, "could not find endpoint\n");
  1423. return -EINVAL;
  1424. }
  1425. err = v4l2_of_parse_endpoint(np, &ep);
  1426. if (err) {
  1427. dev_err(dev, "could not parse endpoint\n");
  1428. goto out;
  1429. }
  1430. switch (ep.bus.parallel.bus_width) {
  1431. case 4:
  1432. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_4;
  1433. break;
  1434. case 5:
  1435. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_5;
  1436. break;
  1437. case 8:
  1438. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_8;
  1439. break;
  1440. case 9:
  1441. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_9;
  1442. break;
  1443. case 10:
  1444. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1445. break;
  1446. default:
  1447. break;
  1448. }
  1449. if (ep.bus.parallel.flags & V4L2_MBUS_MASTER)
  1450. pcdev->platform_flags |= PXA_CAMERA_MASTER;
  1451. if (ep.bus.parallel.flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
  1452. pcdev->platform_flags |= PXA_CAMERA_HSP;
  1453. if (ep.bus.parallel.flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  1454. pcdev->platform_flags |= PXA_CAMERA_VSP;
  1455. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  1456. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN | PXA_CAMERA_PCP;
  1457. if (ep.bus.parallel.flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  1458. pcdev->platform_flags |= PXA_CAMERA_PCLK_EN;
  1459. out:
  1460. of_node_put(np);
  1461. return err;
  1462. }
  1463. static int pxa_camera_probe(struct platform_device *pdev)
  1464. {
  1465. struct pxa_camera_dev *pcdev;
  1466. struct resource *res;
  1467. void __iomem *base;
  1468. int irq;
  1469. int err = 0;
  1470. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1471. irq = platform_get_irq(pdev, 0);
  1472. if (!res || irq < 0)
  1473. return -ENODEV;
  1474. pcdev = devm_kzalloc(&pdev->dev, sizeof(*pcdev), GFP_KERNEL);
  1475. if (!pcdev) {
  1476. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1477. return -ENOMEM;
  1478. }
  1479. pcdev->clk = devm_clk_get(&pdev->dev, NULL);
  1480. if (IS_ERR(pcdev->clk))
  1481. return PTR_ERR(pcdev->clk);
  1482. pcdev->res = res;
  1483. pcdev->pdata = pdev->dev.platform_data;
  1484. if (&pdev->dev.of_node && !pcdev->pdata) {
  1485. err = pxa_camera_pdata_from_dt(&pdev->dev, pcdev);
  1486. } else {
  1487. pcdev->platform_flags = pcdev->pdata->flags;
  1488. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  1489. }
  1490. if (err < 0)
  1491. return err;
  1492. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  1493. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  1494. /*
  1495. * Platform hasn't set available data widths. This is bad.
  1496. * Warn and use a default.
  1497. */
  1498. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  1499. "data widths, using default 10 bit\n");
  1500. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1501. }
  1502. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8)
  1503. pcdev->width_flags = 1 << 7;
  1504. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9)
  1505. pcdev->width_flags |= 1 << 8;
  1506. if (pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10)
  1507. pcdev->width_flags |= 1 << 9;
  1508. if (!pcdev->mclk) {
  1509. dev_warn(&pdev->dev,
  1510. "mclk == 0! Please, fix your platform data. "
  1511. "Using default 20MHz\n");
  1512. pcdev->mclk = 20000000;
  1513. }
  1514. pcdev->mclk_divisor = mclk_get_divisor(pdev, pcdev);
  1515. INIT_LIST_HEAD(&pcdev->capture);
  1516. spin_lock_init(&pcdev->lock);
  1517. /*
  1518. * Request the regions.
  1519. */
  1520. base = devm_ioremap_resource(&pdev->dev, res);
  1521. if (IS_ERR(base))
  1522. return PTR_ERR(base);
  1523. pcdev->irq = irq;
  1524. pcdev->base = base;
  1525. /* request dma */
  1526. err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  1527. pxa_camera_dma_irq_y, pcdev);
  1528. if (err < 0) {
  1529. dev_err(&pdev->dev, "Can't request DMA for Y\n");
  1530. return err;
  1531. }
  1532. pcdev->dma_chans[0] = err;
  1533. dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
  1534. err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
  1535. pxa_camera_dma_irq_u, pcdev);
  1536. if (err < 0) {
  1537. dev_err(&pdev->dev, "Can't request DMA for U\n");
  1538. goto exit_free_dma_y;
  1539. }
  1540. pcdev->dma_chans[1] = err;
  1541. dev_dbg(&pdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
  1542. err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
  1543. pxa_camera_dma_irq_v, pcdev);
  1544. if (err < 0) {
  1545. dev_err(&pdev->dev, "Can't request DMA for V\n");
  1546. goto exit_free_dma_u;
  1547. }
  1548. pcdev->dma_chans[2] = err;
  1549. dev_dbg(&pdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
  1550. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1551. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1552. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1553. /* request irq */
  1554. err = devm_request_irq(&pdev->dev, pcdev->irq, pxa_camera_irq, 0,
  1555. PXA_CAM_DRV_NAME, pcdev);
  1556. if (err) {
  1557. dev_err(&pdev->dev, "Camera interrupt register failed\n");
  1558. goto exit_free_dma;
  1559. }
  1560. pcdev->soc_host.drv_name = PXA_CAM_DRV_NAME;
  1561. pcdev->soc_host.ops = &pxa_soc_camera_host_ops;
  1562. pcdev->soc_host.priv = pcdev;
  1563. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1564. pcdev->soc_host.nr = pdev->id;
  1565. err = soc_camera_host_register(&pcdev->soc_host);
  1566. if (err)
  1567. goto exit_free_dma;
  1568. return 0;
  1569. exit_free_dma:
  1570. pxa_free_dma(pcdev->dma_chans[2]);
  1571. exit_free_dma_u:
  1572. pxa_free_dma(pcdev->dma_chans[1]);
  1573. exit_free_dma_y:
  1574. pxa_free_dma(pcdev->dma_chans[0]);
  1575. return err;
  1576. }
  1577. static int pxa_camera_remove(struct platform_device *pdev)
  1578. {
  1579. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1580. struct pxa_camera_dev *pcdev = container_of(soc_host,
  1581. struct pxa_camera_dev, soc_host);
  1582. pxa_free_dma(pcdev->dma_chans[0]);
  1583. pxa_free_dma(pcdev->dma_chans[1]);
  1584. pxa_free_dma(pcdev->dma_chans[2]);
  1585. soc_camera_host_unregister(soc_host);
  1586. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  1587. return 0;
  1588. }
  1589. static const struct dev_pm_ops pxa_camera_pm = {
  1590. .suspend = pxa_camera_suspend,
  1591. .resume = pxa_camera_resume,
  1592. };
  1593. static const struct of_device_id pxa_camera_of_match[] = {
  1594. { .compatible = "marvell,pxa270-qci", },
  1595. {},
  1596. };
  1597. MODULE_DEVICE_TABLE(of, pxa_camera_of_match);
  1598. static struct platform_driver pxa_camera_driver = {
  1599. .driver = {
  1600. .name = PXA_CAM_DRV_NAME,
  1601. .pm = &pxa_camera_pm,
  1602. .of_match_table = of_match_ptr(pxa_camera_of_match),
  1603. },
  1604. .probe = pxa_camera_probe,
  1605. .remove = pxa_camera_remove,
  1606. };
  1607. module_platform_driver(pxa_camera_driver);
  1608. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  1609. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1610. MODULE_LICENSE("GPL");
  1611. MODULE_VERSION(PXA_CAM_VERSION);
  1612. MODULE_ALIAS("platform:" PXA_CAM_DRV_NAME);