pxa_camera.c 50 KB

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