pxa_camera.c 44 KB

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