pxa_camera.c 26 KB

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