pxa_camera.c 50 KB

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