pxa_camera.c 50 KB

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