pxa_camera.c 48 KB

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