pxa_camera.c 32 KB

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