pxa_camera.c 50 KB

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