pxa_camera.c 48 KB

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