pxa_camera.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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/mutex.h>
  28. #include <linux/clk.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 <asm/dma.h>
  35. #include <mach/pxa-regs.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. #define CICR0_SIM_MP (0 << 24)
  40. #define CICR0_SIM_SP (1 << 24)
  41. #define CICR0_SIM_MS (2 << 24)
  42. #define CICR0_SIM_EP (3 << 24)
  43. #define CICR0_SIM_ES (4 << 24)
  44. #define CICR1_DW_VAL(x) ((x) & CICR1_DW) /* Data bus width */
  45. #define CICR1_PPL_VAL(x) (((x) << 15) & CICR1_PPL) /* Pixels per line */
  46. #define CICR1_COLOR_SP_VAL(x) (((x) << 3) & CICR1_COLOR_SP) /* color space */
  47. #define CICR1_RGB_BPP_VAL(x) (((x) << 7) & CICR1_RGB_BPP) /* bpp for rgb */
  48. #define CICR1_RGBT_CONV_VAL(x) (((x) << 29) & CICR1_RGBT_CONV) /* rgbt conv */
  49. #define CICR2_BLW_VAL(x) (((x) << 24) & CICR2_BLW) /* Beginning-of-line pixel clock wait count */
  50. #define CICR2_ELW_VAL(x) (((x) << 16) & CICR2_ELW) /* End-of-line pixel clock wait count */
  51. #define CICR2_HSW_VAL(x) (((x) << 10) & CICR2_HSW) /* Horizontal sync pulse width */
  52. #define CICR2_BFPW_VAL(x) (((x) << 3) & CICR2_BFPW) /* Beginning-of-frame pixel clock wait count */
  53. #define CICR2_FSW_VAL(x) (((x) << 0) & CICR2_FSW) /* Frame stabilization wait count */
  54. #define CICR3_BFW_VAL(x) (((x) << 24) & CICR3_BFW) /* Beginning-of-frame line clock wait count */
  55. #define CICR3_EFW_VAL(x) (((x) << 16) & CICR3_EFW) /* End-of-frame line clock wait count */
  56. #define CICR3_VSW_VAL(x) (((x) << 11) & CICR3_VSW) /* Vertical sync pulse width */
  57. #define CICR3_LPF_VAL(x) (((x) << 0) & CICR3_LPF) /* Lines per frame */
  58. #define CICR0_IRQ_MASK (CICR0_TOM | CICR0_RDAVM | CICR0_FEM | CICR0_EOLM | \
  59. CICR0_PERRM | CICR0_QDM | CICR0_CDM | CICR0_SOFM | \
  60. CICR0_EOFM | CICR0_FOM)
  61. static DEFINE_MUTEX(camera_lock);
  62. /*
  63. * Structures
  64. */
  65. enum pxa_camera_active_dma {
  66. DMA_Y = 0x1,
  67. DMA_U = 0x2,
  68. DMA_V = 0x4,
  69. };
  70. /* descriptor needed for the PXA DMA engine */
  71. struct pxa_cam_dma {
  72. dma_addr_t sg_dma;
  73. struct pxa_dma_desc *sg_cpu;
  74. size_t sg_size;
  75. int sglen;
  76. };
  77. /* buffer for one video frame */
  78. struct pxa_buffer {
  79. /* common v4l buffer stuff -- must be first */
  80. struct videobuf_buffer vb;
  81. const struct soc_camera_data_format *fmt;
  82. /* our descriptor lists for Y, U and V channels */
  83. struct pxa_cam_dma dmas[3];
  84. int inwork;
  85. enum pxa_camera_active_dma active_dma;
  86. };
  87. struct pxa_camera_dev {
  88. struct device *dev;
  89. /* PXA27x is only supposed to handle one camera on its Quick Capture
  90. * interface. If anyone ever builds hardware to enable more than
  91. * one camera, they will have to modify this driver too */
  92. struct soc_camera_device *icd;
  93. struct clk *clk;
  94. unsigned int irq;
  95. void __iomem *base;
  96. int channels;
  97. unsigned int dma_chans[3];
  98. struct pxacamera_platform_data *pdata;
  99. struct resource *res;
  100. unsigned long platform_flags;
  101. unsigned long platform_mclk_10khz;
  102. struct list_head capture;
  103. spinlock_t lock;
  104. struct pxa_buffer *active;
  105. struct pxa_dma_desc *sg_tail[3];
  106. u32 save_cicr[5];
  107. };
  108. static const char *pxa_cam_driver_description = "PXA_Camera";
  109. static unsigned int vid_limit = 16; /* Video memory limit, in Mb */
  110. /*
  111. * Videobuf operations
  112. */
  113. static int pxa_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  114. unsigned int *size)
  115. {
  116. struct soc_camera_device *icd = vq->priv_data;
  117. struct soc_camera_host *ici =
  118. to_soc_camera_host(icd->dev.parent);
  119. struct pxa_camera_dev *pcdev = ici->priv;
  120. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  121. /* planar capture requires Y, U and V buffers to be page aligned */
  122. if (pcdev->channels == 3) {
  123. *size = PAGE_ALIGN(icd->width * icd->height); /* Y pages */
  124. *size += PAGE_ALIGN(icd->width * icd->height / 2); /* U pages */
  125. *size += PAGE_ALIGN(icd->width * icd->height / 2); /* V pages */
  126. } else {
  127. *size = icd->width * icd->height *
  128. ((icd->current_fmt->depth + 7) >> 3);
  129. }
  130. if (0 == *count)
  131. *count = 32;
  132. while (*size * *count > vid_limit * 1024 * 1024)
  133. (*count)--;
  134. return 0;
  135. }
  136. static void free_buffer(struct videobuf_queue *vq, struct pxa_buffer *buf)
  137. {
  138. struct soc_camera_device *icd = vq->priv_data;
  139. struct soc_camera_host *ici =
  140. to_soc_camera_host(icd->dev.parent);
  141. struct pxa_camera_dev *pcdev = ici->priv;
  142. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  143. int i;
  144. BUG_ON(in_interrupt());
  145. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  146. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  147. /* This waits until this buffer is out of danger, i.e., until it is no
  148. * longer in STATE_QUEUED or STATE_ACTIVE */
  149. videobuf_waiton(&buf->vb, 0, 0);
  150. videobuf_dma_unmap(vq, dma);
  151. videobuf_dma_free(dma);
  152. for (i = 0; i < ARRAY_SIZE(buf->dmas); i++) {
  153. if (buf->dmas[i].sg_cpu)
  154. dma_free_coherent(pcdev->dev, buf->dmas[i].sg_size,
  155. buf->dmas[i].sg_cpu,
  156. buf->dmas[i].sg_dma);
  157. buf->dmas[i].sg_cpu = NULL;
  158. }
  159. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  160. }
  161. static int pxa_init_dma_channel(struct pxa_camera_dev *pcdev,
  162. struct pxa_buffer *buf,
  163. struct videobuf_dmabuf *dma, int channel,
  164. int sglen, int sg_start, int cibr,
  165. unsigned int size)
  166. {
  167. struct pxa_cam_dma *pxa_dma = &buf->dmas[channel];
  168. int i;
  169. if (pxa_dma->sg_cpu)
  170. dma_free_coherent(pcdev->dev, pxa_dma->sg_size,
  171. pxa_dma->sg_cpu, pxa_dma->sg_dma);
  172. pxa_dma->sg_size = (sglen + 1) * sizeof(struct pxa_dma_desc);
  173. pxa_dma->sg_cpu = dma_alloc_coherent(pcdev->dev, pxa_dma->sg_size,
  174. &pxa_dma->sg_dma, GFP_KERNEL);
  175. if (!pxa_dma->sg_cpu)
  176. return -ENOMEM;
  177. pxa_dma->sglen = sglen;
  178. for (i = 0; i < sglen; i++) {
  179. int sg_i = sg_start + i;
  180. struct scatterlist *sg = dma->sglist;
  181. unsigned int dma_len = sg_dma_len(&sg[sg_i]), xfer_len;
  182. pxa_dma->sg_cpu[i].dsadr = pcdev->res->start + cibr;
  183. pxa_dma->sg_cpu[i].dtadr = sg_dma_address(&sg[sg_i]);
  184. /* PXA27x Developer's Manual 27.4.4.1: round up to 8 bytes */
  185. xfer_len = (min(dma_len, size) + 7) & ~7;
  186. pxa_dma->sg_cpu[i].dcmd =
  187. DCMD_FLOWSRC | DCMD_BURST8 | DCMD_INCTRGADDR | xfer_len;
  188. size -= dma_len;
  189. pxa_dma->sg_cpu[i].ddadr =
  190. pxa_dma->sg_dma + (i + 1) * sizeof(struct pxa_dma_desc);
  191. }
  192. pxa_dma->sg_cpu[sglen - 1].ddadr = DDADR_STOP;
  193. pxa_dma->sg_cpu[sglen - 1].dcmd |= DCMD_ENDIRQEN;
  194. return 0;
  195. }
  196. static int pxa_videobuf_prepare(struct videobuf_queue *vq,
  197. struct videobuf_buffer *vb, enum v4l2_field field)
  198. {
  199. struct soc_camera_device *icd = vq->priv_data;
  200. struct soc_camera_host *ici =
  201. to_soc_camera_host(icd->dev.parent);
  202. struct pxa_camera_dev *pcdev = ici->priv;
  203. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  204. int ret;
  205. int sglen_y, sglen_yu = 0, sglen_u = 0, sglen_v = 0;
  206. int size_y, size_u = 0, size_v = 0;
  207. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  208. vb, vb->baddr, vb->bsize);
  209. /* Added list head initialization on alloc */
  210. WARN_ON(!list_empty(&vb->queue));
  211. #ifdef DEBUG
  212. /* This can be useful if you want to see if we actually fill
  213. * the buffer with something */
  214. memset((void *)vb->baddr, 0xaa, vb->bsize);
  215. #endif
  216. BUG_ON(NULL == icd->current_fmt);
  217. /* I think, in buf_prepare you only have to protect global data,
  218. * the actual buffer is yours */
  219. buf->inwork = 1;
  220. if (buf->fmt != icd->current_fmt ||
  221. vb->width != icd->width ||
  222. vb->height != icd->height ||
  223. vb->field != field) {
  224. buf->fmt = icd->current_fmt;
  225. vb->width = icd->width;
  226. vb->height = icd->height;
  227. vb->field = field;
  228. vb->state = VIDEOBUF_NEEDS_INIT;
  229. }
  230. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  231. if (0 != vb->baddr && vb->bsize < vb->size) {
  232. ret = -EINVAL;
  233. goto out;
  234. }
  235. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  236. unsigned int size = vb->size;
  237. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  238. ret = videobuf_iolock(vq, vb, NULL);
  239. if (ret)
  240. goto fail;
  241. if (pcdev->channels == 3) {
  242. /* FIXME the calculations should be more precise */
  243. sglen_y = dma->sglen / 2;
  244. sglen_u = sglen_v = dma->sglen / 4 + 1;
  245. sglen_yu = sglen_y + sglen_u;
  246. size_y = size / 2;
  247. size_u = size_v = size / 4;
  248. } else {
  249. sglen_y = dma->sglen;
  250. size_y = size;
  251. }
  252. /* init DMA for Y channel */
  253. ret = pxa_init_dma_channel(pcdev, buf, dma, 0, sglen_y,
  254. 0, 0x28, size_y);
  255. if (ret) {
  256. dev_err(pcdev->dev,
  257. "DMA initialization for Y/RGB failed\n");
  258. goto fail;
  259. }
  260. if (pcdev->channels == 3) {
  261. /* init DMA for U channel */
  262. ret = pxa_init_dma_channel(pcdev, buf, dma, 1, sglen_u,
  263. sglen_y, 0x30, size_u);
  264. if (ret) {
  265. dev_err(pcdev->dev,
  266. "DMA initialization for U failed\n");
  267. goto fail_u;
  268. }
  269. /* init DMA for V channel */
  270. ret = pxa_init_dma_channel(pcdev, buf, dma, 2, sglen_v,
  271. sglen_yu, 0x38, size_v);
  272. if (ret) {
  273. dev_err(pcdev->dev,
  274. "DMA initialization for V failed\n");
  275. goto fail_v;
  276. }
  277. }
  278. vb->state = VIDEOBUF_PREPARED;
  279. }
  280. buf->inwork = 0;
  281. buf->active_dma = DMA_Y;
  282. if (pcdev->channels == 3)
  283. buf->active_dma |= DMA_U | DMA_V;
  284. return 0;
  285. fail_v:
  286. dma_free_coherent(pcdev->dev, buf->dmas[1].sg_size,
  287. buf->dmas[1].sg_cpu, buf->dmas[1].sg_dma);
  288. fail_u:
  289. dma_free_coherent(pcdev->dev, buf->dmas[0].sg_size,
  290. buf->dmas[0].sg_cpu, buf->dmas[0].sg_dma);
  291. fail:
  292. free_buffer(vq, buf);
  293. out:
  294. buf->inwork = 0;
  295. return ret;
  296. }
  297. static void pxa_videobuf_queue(struct videobuf_queue *vq,
  298. struct videobuf_buffer *vb)
  299. {
  300. struct soc_camera_device *icd = vq->priv_data;
  301. struct soc_camera_host *ici =
  302. to_soc_camera_host(icd->dev.parent);
  303. struct pxa_camera_dev *pcdev = ici->priv;
  304. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  305. struct pxa_buffer *active;
  306. unsigned long flags;
  307. int i;
  308. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  309. vb, vb->baddr, vb->bsize);
  310. spin_lock_irqsave(&pcdev->lock, flags);
  311. list_add_tail(&vb->queue, &pcdev->capture);
  312. vb->state = VIDEOBUF_ACTIVE;
  313. active = pcdev->active;
  314. if (!active) {
  315. CIFR |= CIFR_RESET_F;
  316. for (i = 0; i < pcdev->channels; i++) {
  317. DDADR(pcdev->dma_chans[i]) = buf->dmas[i].sg_dma;
  318. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  319. pcdev->sg_tail[i] = buf->dmas[i].sg_cpu + buf->dmas[i].sglen - 1;
  320. }
  321. pcdev->active = buf;
  322. CICR0 |= CICR0_ENB;
  323. } else {
  324. struct pxa_cam_dma *buf_dma;
  325. struct pxa_cam_dma *act_dma;
  326. int nents;
  327. for (i = 0; i < pcdev->channels; i++) {
  328. buf_dma = &buf->dmas[i];
  329. act_dma = &active->dmas[i];
  330. nents = buf_dma->sglen;
  331. /* Stop DMA engine */
  332. DCSR(pcdev->dma_chans[i]) = 0;
  333. /* Add the descriptors we just initialized to
  334. the currently running chain */
  335. pcdev->sg_tail[i]->ddadr = buf_dma->sg_dma;
  336. pcdev->sg_tail[i] = buf_dma->sg_cpu + buf_dma->sglen - 1;
  337. /* Setup a dummy descriptor with the DMA engines current
  338. * state
  339. */
  340. buf_dma->sg_cpu[nents].dsadr =
  341. pcdev->res->start + 0x28 + i*8; /* CIBRx */
  342. buf_dma->sg_cpu[nents].dtadr =
  343. DTADR(pcdev->dma_chans[i]);
  344. buf_dma->sg_cpu[nents].dcmd =
  345. DCMD(pcdev->dma_chans[i]);
  346. if (DDADR(pcdev->dma_chans[i]) == DDADR_STOP) {
  347. /* The DMA engine is on the last
  348. descriptor, set the next descriptors
  349. address to the descriptors we just
  350. initialized */
  351. buf_dma->sg_cpu[nents].ddadr = buf_dma->sg_dma;
  352. } else {
  353. buf_dma->sg_cpu[nents].ddadr =
  354. DDADR(pcdev->dma_chans[i]);
  355. }
  356. /* The next descriptor is the dummy descriptor */
  357. DDADR(pcdev->dma_chans[i]) = buf_dma->sg_dma + nents *
  358. sizeof(struct pxa_dma_desc);
  359. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  360. }
  361. }
  362. spin_unlock_irqrestore(&pcdev->lock, flags);
  363. }
  364. static void pxa_videobuf_release(struct videobuf_queue *vq,
  365. struct videobuf_buffer *vb)
  366. {
  367. struct pxa_buffer *buf = container_of(vb, struct pxa_buffer, vb);
  368. #ifdef DEBUG
  369. struct soc_camera_device *icd = vq->priv_data;
  370. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  371. vb, vb->baddr, vb->bsize);
  372. switch (vb->state) {
  373. case VIDEOBUF_ACTIVE:
  374. dev_dbg(&icd->dev, "%s (active)\n", __func__);
  375. break;
  376. case VIDEOBUF_QUEUED:
  377. dev_dbg(&icd->dev, "%s (queued)\n", __func__);
  378. break;
  379. case VIDEOBUF_PREPARED:
  380. dev_dbg(&icd->dev, "%s (prepared)\n", __func__);
  381. break;
  382. default:
  383. dev_dbg(&icd->dev, "%s (unknown)\n", __func__);
  384. break;
  385. }
  386. #endif
  387. free_buffer(vq, buf);
  388. }
  389. static void pxa_camera_wakeup(struct pxa_camera_dev *pcdev,
  390. struct videobuf_buffer *vb,
  391. struct pxa_buffer *buf)
  392. {
  393. /* _init is used to debug races, see comment in pxa_camera_reqbufs() */
  394. list_del_init(&vb->queue);
  395. vb->state = VIDEOBUF_DONE;
  396. do_gettimeofday(&vb->ts);
  397. vb->field_count++;
  398. wake_up(&vb->done);
  399. if (list_empty(&pcdev->capture)) {
  400. pcdev->active = NULL;
  401. DCSR(pcdev->dma_chans[0]) = 0;
  402. DCSR(pcdev->dma_chans[1]) = 0;
  403. DCSR(pcdev->dma_chans[2]) = 0;
  404. CICR0 &= ~CICR0_ENB;
  405. return;
  406. }
  407. pcdev->active = list_entry(pcdev->capture.next,
  408. struct pxa_buffer, vb.queue);
  409. }
  410. static void pxa_camera_dma_irq(int channel, struct pxa_camera_dev *pcdev,
  411. enum pxa_camera_active_dma act_dma)
  412. {
  413. struct pxa_buffer *buf;
  414. unsigned long flags;
  415. u32 status, camera_status, overrun;
  416. struct videobuf_buffer *vb;
  417. spin_lock_irqsave(&pcdev->lock, flags);
  418. status = DCSR(channel);
  419. DCSR(channel) = status | DCSR_ENDINTR;
  420. if (status & DCSR_BUSERR) {
  421. dev_err(pcdev->dev, "DMA Bus Error IRQ!\n");
  422. goto out;
  423. }
  424. if (!(status & DCSR_ENDINTR)) {
  425. dev_err(pcdev->dev, "Unknown DMA IRQ source, "
  426. "status: 0x%08x\n", status);
  427. goto out;
  428. }
  429. if (!pcdev->active) {
  430. dev_err(pcdev->dev, "DMA End IRQ with no active buffer!\n");
  431. goto out;
  432. }
  433. camera_status = CISR;
  434. overrun = CISR_IFO_0;
  435. if (pcdev->channels == 3)
  436. overrun |= CISR_IFO_1 | CISR_IFO_2;
  437. if (camera_status & overrun) {
  438. dev_dbg(pcdev->dev, "FIFO overrun! CISR: %x\n", camera_status);
  439. /* Stop the Capture Interface */
  440. CICR0 &= ~CICR0_ENB;
  441. /* Stop DMA */
  442. DCSR(channel) = 0;
  443. /* Reset the FIFOs */
  444. CIFR |= CIFR_RESET_F;
  445. /* Enable End-Of-Frame Interrupt */
  446. CICR0 &= ~CICR0_EOFM;
  447. /* Restart the Capture Interface */
  448. CICR0 |= CICR0_ENB;
  449. goto out;
  450. }
  451. vb = &pcdev->active->vb;
  452. buf = container_of(vb, struct pxa_buffer, vb);
  453. WARN_ON(buf->inwork || list_empty(&vb->queue));
  454. dev_dbg(pcdev->dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  455. vb, vb->baddr, vb->bsize);
  456. buf->active_dma &= ~act_dma;
  457. if (!buf->active_dma)
  458. pxa_camera_wakeup(pcdev, vb, buf);
  459. out:
  460. spin_unlock_irqrestore(&pcdev->lock, flags);
  461. }
  462. static void pxa_camera_dma_irq_y(int channel, void *data)
  463. {
  464. struct pxa_camera_dev *pcdev = data;
  465. pxa_camera_dma_irq(channel, pcdev, DMA_Y);
  466. }
  467. static void pxa_camera_dma_irq_u(int channel, void *data)
  468. {
  469. struct pxa_camera_dev *pcdev = data;
  470. pxa_camera_dma_irq(channel, pcdev, DMA_U);
  471. }
  472. static void pxa_camera_dma_irq_v(int channel, void *data)
  473. {
  474. struct pxa_camera_dev *pcdev = data;
  475. pxa_camera_dma_irq(channel, pcdev, DMA_V);
  476. }
  477. static struct videobuf_queue_ops pxa_videobuf_ops = {
  478. .buf_setup = pxa_videobuf_setup,
  479. .buf_prepare = pxa_videobuf_prepare,
  480. .buf_queue = pxa_videobuf_queue,
  481. .buf_release = pxa_videobuf_release,
  482. };
  483. static void pxa_camera_init_videobuf(struct videobuf_queue *q,
  484. struct soc_camera_device *icd)
  485. {
  486. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  487. struct pxa_camera_dev *pcdev = ici->priv;
  488. /* We must pass NULL as dev pointer, then all pci_* dma operations
  489. * transform to normal dma_* ones. */
  490. videobuf_queue_sg_init(q, &pxa_videobuf_ops, NULL, &pcdev->lock,
  491. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  492. sizeof(struct pxa_buffer), icd);
  493. }
  494. static int mclk_get_divisor(struct pxa_camera_dev *pcdev)
  495. {
  496. unsigned int mclk_10khz = pcdev->platform_mclk_10khz;
  497. unsigned long div;
  498. unsigned long lcdclk;
  499. lcdclk = clk_get_rate(pcdev->clk) / 10000;
  500. /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
  501. * they get a nice Oops */
  502. div = (lcdclk + 2 * mclk_10khz - 1) / (2 * mclk_10khz) - 1;
  503. dev_dbg(pcdev->dev, "LCD clock %lukHz, target freq %dkHz, "
  504. "divisor %lu\n", lcdclk * 10, mclk_10khz * 10, div);
  505. return div;
  506. }
  507. static void pxa_camera_activate(struct pxa_camera_dev *pcdev)
  508. {
  509. struct pxacamera_platform_data *pdata = pcdev->pdata;
  510. u32 cicr4 = 0;
  511. dev_dbg(pcdev->dev, "Registered platform device at %p data %p\n",
  512. pcdev, pdata);
  513. if (pdata && pdata->init) {
  514. dev_dbg(pcdev->dev, "%s: Init gpios\n", __func__);
  515. pdata->init(pcdev->dev);
  516. }
  517. CICR0 = 0x3FF; /* disable all interrupts */
  518. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  519. cicr4 |= CICR4_PCLK_EN;
  520. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  521. cicr4 |= CICR4_MCLK_EN;
  522. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  523. cicr4 |= CICR4_PCP;
  524. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  525. cicr4 |= CICR4_HSP;
  526. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  527. cicr4 |= CICR4_VSP;
  528. CICR4 = mclk_get_divisor(pcdev) | cicr4;
  529. clk_enable(pcdev->clk);
  530. }
  531. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  532. {
  533. clk_disable(pcdev->clk);
  534. }
  535. static irqreturn_t pxa_camera_irq(int irq, void *data)
  536. {
  537. struct pxa_camera_dev *pcdev = data;
  538. unsigned int status = CISR;
  539. dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
  540. if (!status)
  541. return IRQ_NONE;
  542. CISR = status;
  543. if (status & CISR_EOF) {
  544. int i;
  545. for (i = 0; i < pcdev->channels; i++) {
  546. DDADR(pcdev->dma_chans[i]) =
  547. pcdev->active->dmas[i].sg_dma;
  548. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  549. }
  550. CICR0 |= CICR0_EOFM;
  551. }
  552. return IRQ_HANDLED;
  553. }
  554. /* The following two functions absolutely depend on the fact, that
  555. * there can be only one camera on PXA quick capture interface */
  556. static int pxa_camera_add_device(struct soc_camera_device *icd)
  557. {
  558. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  559. struct pxa_camera_dev *pcdev = ici->priv;
  560. int ret;
  561. mutex_lock(&camera_lock);
  562. if (pcdev->icd) {
  563. ret = -EBUSY;
  564. goto ebusy;
  565. }
  566. dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
  567. icd->devnum);
  568. pxa_camera_activate(pcdev);
  569. ret = icd->ops->init(icd);
  570. if (!ret)
  571. pcdev->icd = icd;
  572. ebusy:
  573. mutex_unlock(&camera_lock);
  574. return ret;
  575. }
  576. static void pxa_camera_remove_device(struct soc_camera_device *icd)
  577. {
  578. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  579. struct pxa_camera_dev *pcdev = ici->priv;
  580. BUG_ON(icd != pcdev->icd);
  581. dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
  582. icd->devnum);
  583. /* disable capture, disable interrupts */
  584. CICR0 = 0x3ff;
  585. /* Stop DMA engine */
  586. DCSR(pcdev->dma_chans[0]) = 0;
  587. DCSR(pcdev->dma_chans[1]) = 0;
  588. DCSR(pcdev->dma_chans[2]) = 0;
  589. icd->ops->release(icd);
  590. pxa_camera_deactivate(pcdev);
  591. pcdev->icd = NULL;
  592. }
  593. static int test_platform_param(struct pxa_camera_dev *pcdev,
  594. unsigned char buswidth, unsigned long *flags)
  595. {
  596. /*
  597. * Platform specified synchronization and pixel clock polarities are
  598. * only a recommendation and are only used during probing. The PXA270
  599. * quick capture interface supports both.
  600. */
  601. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  602. SOCAM_MASTER : SOCAM_SLAVE) |
  603. SOCAM_HSYNC_ACTIVE_HIGH |
  604. SOCAM_HSYNC_ACTIVE_LOW |
  605. SOCAM_VSYNC_ACTIVE_HIGH |
  606. SOCAM_VSYNC_ACTIVE_LOW |
  607. SOCAM_PCLK_SAMPLE_RISING |
  608. SOCAM_PCLK_SAMPLE_FALLING;
  609. /* If requested data width is supported by the platform, use it */
  610. switch (buswidth) {
  611. case 10:
  612. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
  613. return -EINVAL;
  614. *flags |= SOCAM_DATAWIDTH_10;
  615. break;
  616. case 9:
  617. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
  618. return -EINVAL;
  619. *flags |= SOCAM_DATAWIDTH_9;
  620. break;
  621. case 8:
  622. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
  623. return -EINVAL;
  624. *flags |= SOCAM_DATAWIDTH_8;
  625. break;
  626. default:
  627. return -EINVAL;
  628. }
  629. return 0;
  630. }
  631. static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  632. {
  633. struct soc_camera_host *ici =
  634. to_soc_camera_host(icd->dev.parent);
  635. struct pxa_camera_dev *pcdev = ici->priv;
  636. unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
  637. u32 cicr0, cicr1, cicr4 = 0;
  638. int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
  639. if (ret < 0)
  640. return ret;
  641. camera_flags = icd->ops->query_bus_param(icd);
  642. common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
  643. if (!common_flags)
  644. return -EINVAL;
  645. pcdev->channels = 1;
  646. /* Make choises, based on platform preferences */
  647. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  648. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  649. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  650. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  651. else
  652. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  653. }
  654. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  655. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  656. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  657. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  658. else
  659. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  660. }
  661. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  662. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  663. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  664. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  665. else
  666. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  667. }
  668. ret = icd->ops->set_bus_param(icd, common_flags);
  669. if (ret < 0)
  670. return ret;
  671. /* Datawidth is now guaranteed to be equal to one of the three values.
  672. * We fix bit-per-pixel equal to data-width... */
  673. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  674. case SOCAM_DATAWIDTH_10:
  675. dw = 4;
  676. bpp = 0x40;
  677. break;
  678. case SOCAM_DATAWIDTH_9:
  679. dw = 3;
  680. bpp = 0x20;
  681. break;
  682. default:
  683. /* Actually it can only be 8 now,
  684. * default is just to silence compiler warnings */
  685. case SOCAM_DATAWIDTH_8:
  686. dw = 2;
  687. bpp = 0;
  688. }
  689. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  690. cicr4 |= CICR4_PCLK_EN;
  691. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  692. cicr4 |= CICR4_MCLK_EN;
  693. if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
  694. cicr4 |= CICR4_PCP;
  695. if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
  696. cicr4 |= CICR4_HSP;
  697. if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
  698. cicr4 |= CICR4_VSP;
  699. cicr0 = CICR0;
  700. if (cicr0 & CICR0_ENB)
  701. CICR0 = cicr0 & ~CICR0_ENB;
  702. cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
  703. switch (pixfmt) {
  704. case V4L2_PIX_FMT_YUV422P:
  705. pcdev->channels = 3;
  706. cicr1 |= CICR1_YCBCR_F;
  707. /*
  708. * Normally, pxa bus wants as input UYVY format. We allow all
  709. * reorderings of the YUV422 format, as no processing is done,
  710. * and the YUV stream is just passed through without any
  711. * transformation. Note that UYVY is the only format that
  712. * should be used if pxa framebuffer Overlay2 is used.
  713. */
  714. case V4L2_PIX_FMT_UYVY:
  715. case V4L2_PIX_FMT_VYUY:
  716. case V4L2_PIX_FMT_YUYV:
  717. case V4L2_PIX_FMT_YVYU:
  718. cicr1 |= CICR1_COLOR_SP_VAL(2);
  719. break;
  720. case V4L2_PIX_FMT_RGB555:
  721. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  722. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  723. break;
  724. case V4L2_PIX_FMT_RGB565:
  725. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  726. break;
  727. }
  728. CICR1 = cicr1;
  729. CICR2 = 0;
  730. CICR3 = CICR3_LPF_VAL(icd->height - 1) |
  731. CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
  732. CICR4 = mclk_get_divisor(pcdev) | cicr4;
  733. /* CIF interrupts are not used, only DMA */
  734. CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  735. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
  736. CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
  737. return 0;
  738. }
  739. static int pxa_camera_try_bus_param(struct soc_camera_device *icd,
  740. unsigned char buswidth)
  741. {
  742. struct soc_camera_host *ici =
  743. to_soc_camera_host(icd->dev.parent);
  744. struct pxa_camera_dev *pcdev = ici->priv;
  745. unsigned long bus_flags, camera_flags;
  746. int ret = test_platform_param(pcdev, buswidth, &bus_flags);
  747. if (ret < 0)
  748. return ret;
  749. camera_flags = icd->ops->query_bus_param(icd);
  750. return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
  751. }
  752. static const struct soc_camera_data_format pxa_camera_formats[] = {
  753. {
  754. .name = "Planar YUV422 16 bit",
  755. .depth = 16,
  756. .fourcc = V4L2_PIX_FMT_YUV422P,
  757. .colorspace = V4L2_COLORSPACE_JPEG,
  758. },
  759. };
  760. static bool buswidth_supported(struct soc_camera_device *icd, int depth)
  761. {
  762. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  763. struct pxa_camera_dev *pcdev = ici->priv;
  764. switch (depth) {
  765. case 8:
  766. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8);
  767. case 9:
  768. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9);
  769. case 10:
  770. return !!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10);
  771. }
  772. return false;
  773. }
  774. static int required_buswidth(const struct soc_camera_data_format *fmt)
  775. {
  776. switch (fmt->fourcc) {
  777. case V4L2_PIX_FMT_UYVY:
  778. case V4L2_PIX_FMT_VYUY:
  779. case V4L2_PIX_FMT_YUYV:
  780. case V4L2_PIX_FMT_YVYU:
  781. case V4L2_PIX_FMT_RGB565:
  782. case V4L2_PIX_FMT_RGB555:
  783. return 8;
  784. default:
  785. return fmt->depth;
  786. }
  787. }
  788. static int pxa_camera_get_formats(struct soc_camera_device *icd, int idx,
  789. struct soc_camera_format_xlate *xlate)
  790. {
  791. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  792. int formats = 0, buswidth, ret;
  793. buswidth = required_buswidth(icd->formats + idx);
  794. if (!buswidth_supported(icd, buswidth))
  795. return 0;
  796. ret = pxa_camera_try_bus_param(icd, buswidth);
  797. if (ret < 0)
  798. return 0;
  799. switch (icd->formats[idx].fourcc) {
  800. case V4L2_PIX_FMT_UYVY:
  801. formats++;
  802. if (xlate) {
  803. xlate->host_fmt = &pxa_camera_formats[0];
  804. xlate->cam_fmt = icd->formats + idx;
  805. xlate->buswidth = buswidth;
  806. xlate++;
  807. dev_dbg(&ici->dev, "Providing format %s using %s\n",
  808. pxa_camera_formats[0].name,
  809. icd->formats[idx].name);
  810. }
  811. case V4L2_PIX_FMT_VYUY:
  812. case V4L2_PIX_FMT_YUYV:
  813. case V4L2_PIX_FMT_YVYU:
  814. case V4L2_PIX_FMT_RGB565:
  815. case V4L2_PIX_FMT_RGB555:
  816. formats++;
  817. if (xlate) {
  818. xlate->host_fmt = icd->formats + idx;
  819. xlate->cam_fmt = icd->formats + idx;
  820. xlate->buswidth = buswidth;
  821. xlate++;
  822. dev_dbg(&ici->dev, "Providing format %s packed\n",
  823. icd->formats[idx].name);
  824. }
  825. break;
  826. default:
  827. /* Generic pass-through */
  828. formats++;
  829. if (xlate) {
  830. xlate->host_fmt = icd->formats + idx;
  831. xlate->cam_fmt = icd->formats + idx;
  832. xlate->buswidth = icd->formats[idx].depth;
  833. xlate++;
  834. dev_dbg(&ici->dev,
  835. "Providing format %s in pass-through mode\n",
  836. icd->formats[idx].name);
  837. }
  838. }
  839. return formats;
  840. }
  841. static int pxa_camera_set_fmt(struct soc_camera_device *icd,
  842. __u32 pixfmt, struct v4l2_rect *rect)
  843. {
  844. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  845. const struct soc_camera_data_format *host_fmt, *cam_fmt = NULL;
  846. const struct soc_camera_format_xlate *xlate;
  847. int ret, buswidth;
  848. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  849. if (!xlate) {
  850. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  851. return -EINVAL;
  852. }
  853. buswidth = xlate->buswidth;
  854. host_fmt = xlate->host_fmt;
  855. cam_fmt = xlate->cam_fmt;
  856. switch (pixfmt) {
  857. case 0: /* Only geometry change */
  858. ret = icd->ops->set_fmt(icd, pixfmt, rect);
  859. break;
  860. default:
  861. ret = icd->ops->set_fmt(icd, cam_fmt->fourcc, rect);
  862. }
  863. if (ret < 0)
  864. dev_warn(&ici->dev, "Failed to configure for format %x\n",
  865. pixfmt);
  866. if (pixfmt && !ret) {
  867. icd->buswidth = buswidth;
  868. icd->current_fmt = host_fmt;
  869. }
  870. return ret;
  871. }
  872. static int pxa_camera_try_fmt(struct soc_camera_device *icd,
  873. struct v4l2_format *f)
  874. {
  875. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  876. const struct soc_camera_format_xlate *xlate;
  877. struct v4l2_pix_format *pix = &f->fmt.pix;
  878. __u32 pixfmt = pix->pixelformat;
  879. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  880. if (!xlate) {
  881. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  882. return -EINVAL;
  883. }
  884. /* limit to pxa hardware capabilities */
  885. if (pix->height < 32)
  886. pix->height = 32;
  887. if (pix->height > 2048)
  888. pix->height = 2048;
  889. if (pix->width < 48)
  890. pix->width = 48;
  891. if (pix->width > 2048)
  892. pix->width = 2048;
  893. pix->width &= ~0x01;
  894. pix->bytesperline = pix->width *
  895. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  896. pix->sizeimage = pix->height * pix->bytesperline;
  897. /* limit to sensor capabilities */
  898. return icd->ops->try_fmt(icd, f);
  899. }
  900. static int pxa_camera_reqbufs(struct soc_camera_file *icf,
  901. struct v4l2_requestbuffers *p)
  902. {
  903. int i;
  904. /* This is for locking debugging only. I removed spinlocks and now I
  905. * check whether .prepare is ever called on a linked buffer, or whether
  906. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  907. * it hadn't triggered */
  908. for (i = 0; i < p->count; i++) {
  909. struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
  910. struct pxa_buffer, vb);
  911. buf->inwork = 0;
  912. INIT_LIST_HEAD(&buf->vb.queue);
  913. }
  914. return 0;
  915. }
  916. static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
  917. {
  918. struct soc_camera_file *icf = file->private_data;
  919. struct pxa_buffer *buf;
  920. buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
  921. vb.stream);
  922. poll_wait(file, &buf->vb.done, pt);
  923. if (buf->vb.state == VIDEOBUF_DONE ||
  924. buf->vb.state == VIDEOBUF_ERROR)
  925. return POLLIN|POLLRDNORM;
  926. return 0;
  927. }
  928. static int pxa_camera_querycap(struct soc_camera_host *ici,
  929. struct v4l2_capability *cap)
  930. {
  931. /* cap->name is set by the firendly caller:-> */
  932. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  933. cap->version = PXA_CAM_VERSION_CODE;
  934. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  935. return 0;
  936. }
  937. static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
  938. {
  939. struct soc_camera_host *ici =
  940. to_soc_camera_host(icd->dev.parent);
  941. struct pxa_camera_dev *pcdev = ici->priv;
  942. int i = 0, ret = 0;
  943. pcdev->save_cicr[i++] = CICR0;
  944. pcdev->save_cicr[i++] = CICR1;
  945. pcdev->save_cicr[i++] = CICR2;
  946. pcdev->save_cicr[i++] = CICR3;
  947. pcdev->save_cicr[i++] = CICR4;
  948. if ((pcdev->icd) && (pcdev->icd->ops->suspend))
  949. ret = pcdev->icd->ops->suspend(pcdev->icd, state);
  950. return ret;
  951. }
  952. static int pxa_camera_resume(struct soc_camera_device *icd)
  953. {
  954. struct soc_camera_host *ici =
  955. to_soc_camera_host(icd->dev.parent);
  956. struct pxa_camera_dev *pcdev = ici->priv;
  957. int i = 0, ret = 0;
  958. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  959. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  960. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  961. CICR0 = pcdev->save_cicr[i++] & ~CICR0_ENB;
  962. CICR1 = pcdev->save_cicr[i++];
  963. CICR2 = pcdev->save_cicr[i++];
  964. CICR3 = pcdev->save_cicr[i++];
  965. CICR4 = pcdev->save_cicr[i++];
  966. if ((pcdev->icd) && (pcdev->icd->ops->resume))
  967. ret = pcdev->icd->ops->resume(pcdev->icd);
  968. /* Restart frame capture if active buffer exists */
  969. if (!ret && pcdev->active) {
  970. /* Reset the FIFOs */
  971. CIFR |= CIFR_RESET_F;
  972. /* Enable End-Of-Frame Interrupt */
  973. CICR0 &= ~CICR0_EOFM;
  974. /* Restart the Capture Interface */
  975. CICR0 |= CICR0_ENB;
  976. }
  977. return ret;
  978. }
  979. static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
  980. .owner = THIS_MODULE,
  981. .add = pxa_camera_add_device,
  982. .remove = pxa_camera_remove_device,
  983. .suspend = pxa_camera_suspend,
  984. .resume = pxa_camera_resume,
  985. .get_formats = pxa_camera_get_formats,
  986. .set_fmt = pxa_camera_set_fmt,
  987. .try_fmt = pxa_camera_try_fmt,
  988. .init_videobuf = pxa_camera_init_videobuf,
  989. .reqbufs = pxa_camera_reqbufs,
  990. .poll = pxa_camera_poll,
  991. .querycap = pxa_camera_querycap,
  992. .set_bus_param = pxa_camera_set_bus_param,
  993. };
  994. /* Should be allocated dynamically too, but we have only one. */
  995. static struct soc_camera_host pxa_soc_camera_host = {
  996. .drv_name = PXA_CAM_DRV_NAME,
  997. .ops = &pxa_soc_camera_host_ops,
  998. };
  999. static int pxa_camera_probe(struct platform_device *pdev)
  1000. {
  1001. struct pxa_camera_dev *pcdev;
  1002. struct resource *res;
  1003. void __iomem *base;
  1004. int irq;
  1005. int err = 0;
  1006. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1007. irq = platform_get_irq(pdev, 0);
  1008. if (!res || irq < 0) {
  1009. err = -ENODEV;
  1010. goto exit;
  1011. }
  1012. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1013. if (!pcdev) {
  1014. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1015. err = -ENOMEM;
  1016. goto exit;
  1017. }
  1018. pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
  1019. if (IS_ERR(pcdev->clk)) {
  1020. err = PTR_ERR(pcdev->clk);
  1021. goto exit_kfree;
  1022. }
  1023. dev_set_drvdata(&pdev->dev, pcdev);
  1024. pcdev->res = res;
  1025. pcdev->pdata = pdev->dev.platform_data;
  1026. pcdev->platform_flags = pcdev->pdata->flags;
  1027. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  1028. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  1029. /* Platform hasn't set available data widths. This is bad.
  1030. * Warn and use a default. */
  1031. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  1032. "data widths, using default 10 bit\n");
  1033. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  1034. }
  1035. pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
  1036. if (!pcdev->platform_mclk_10khz) {
  1037. dev_warn(&pdev->dev,
  1038. "mclk_10khz == 0! Please, fix your platform data. "
  1039. "Using default 20MHz\n");
  1040. pcdev->platform_mclk_10khz = 2000;
  1041. }
  1042. INIT_LIST_HEAD(&pcdev->capture);
  1043. spin_lock_init(&pcdev->lock);
  1044. /*
  1045. * Request the regions.
  1046. */
  1047. if (!request_mem_region(res->start, res->end - res->start + 1,
  1048. PXA_CAM_DRV_NAME)) {
  1049. err = -EBUSY;
  1050. goto exit_clk;
  1051. }
  1052. base = ioremap(res->start, res->end - res->start + 1);
  1053. if (!base) {
  1054. err = -ENOMEM;
  1055. goto exit_release;
  1056. }
  1057. pcdev->irq = irq;
  1058. pcdev->base = base;
  1059. pcdev->dev = &pdev->dev;
  1060. /* request dma */
  1061. err = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  1062. pxa_camera_dma_irq_y, pcdev);
  1063. if (err < 0) {
  1064. dev_err(pcdev->dev, "Can't request DMA for Y\n");
  1065. goto exit_iounmap;
  1066. }
  1067. pcdev->dma_chans[0] = err;
  1068. dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
  1069. err = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
  1070. pxa_camera_dma_irq_u, pcdev);
  1071. if (err < 0) {
  1072. dev_err(pcdev->dev, "Can't request DMA for U\n");
  1073. goto exit_free_dma_y;
  1074. }
  1075. pcdev->dma_chans[1] = err;
  1076. dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
  1077. err = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
  1078. pxa_camera_dma_irq_v, pcdev);
  1079. if (err < 0) {
  1080. dev_err(pcdev->dev, "Can't request DMA for V\n");
  1081. goto exit_free_dma_u;
  1082. }
  1083. pcdev->dma_chans[2] = err;
  1084. dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
  1085. DRCMR(68) = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  1086. DRCMR(69) = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  1087. DRCMR(70) = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  1088. /* request irq */
  1089. err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
  1090. pcdev);
  1091. if (err) {
  1092. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  1093. goto exit_free_dma;
  1094. }
  1095. pxa_soc_camera_host.priv = pcdev;
  1096. pxa_soc_camera_host.dev.parent = &pdev->dev;
  1097. pxa_soc_camera_host.nr = pdev->id;
  1098. err = soc_camera_host_register(&pxa_soc_camera_host);
  1099. if (err)
  1100. goto exit_free_irq;
  1101. return 0;
  1102. exit_free_irq:
  1103. free_irq(pcdev->irq, pcdev);
  1104. exit_free_dma:
  1105. pxa_free_dma(pcdev->dma_chans[2]);
  1106. exit_free_dma_u:
  1107. pxa_free_dma(pcdev->dma_chans[1]);
  1108. exit_free_dma_y:
  1109. pxa_free_dma(pcdev->dma_chans[0]);
  1110. exit_iounmap:
  1111. iounmap(base);
  1112. exit_release:
  1113. release_mem_region(res->start, res->end - res->start + 1);
  1114. exit_clk:
  1115. clk_put(pcdev->clk);
  1116. exit_kfree:
  1117. kfree(pcdev);
  1118. exit:
  1119. return err;
  1120. }
  1121. static int __devexit pxa_camera_remove(struct platform_device *pdev)
  1122. {
  1123. struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
  1124. struct resource *res;
  1125. clk_put(pcdev->clk);
  1126. pxa_free_dma(pcdev->dma_chans[0]);
  1127. pxa_free_dma(pcdev->dma_chans[1]);
  1128. pxa_free_dma(pcdev->dma_chans[2]);
  1129. free_irq(pcdev->irq, pcdev);
  1130. soc_camera_host_unregister(&pxa_soc_camera_host);
  1131. iounmap(pcdev->base);
  1132. res = pcdev->res;
  1133. release_mem_region(res->start, res->end - res->start + 1);
  1134. kfree(pcdev);
  1135. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  1136. return 0;
  1137. }
  1138. static struct platform_driver pxa_camera_driver = {
  1139. .driver = {
  1140. .name = PXA_CAM_DRV_NAME,
  1141. },
  1142. .probe = pxa_camera_probe,
  1143. .remove = __exit_p(pxa_camera_remove),
  1144. };
  1145. static int __devinit pxa_camera_init(void)
  1146. {
  1147. return platform_driver_register(&pxa_camera_driver);
  1148. }
  1149. static void __exit pxa_camera_exit(void)
  1150. {
  1151. platform_driver_unregister(&pxa_camera_driver);
  1152. }
  1153. module_init(pxa_camera_init);
  1154. module_exit(pxa_camera_exit);
  1155. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  1156. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1157. MODULE_LICENSE("GPL");