pxa_camera.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265
  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 <asm/arch/pxa-regs.h>
  36. #include <asm/arch/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. if (pdata && pdata->power) {
  518. dev_dbg(pcdev->dev, "%s: Power on camera\n", __func__);
  519. pdata->power(pcdev->dev, 1);
  520. }
  521. if (pdata && pdata->reset) {
  522. dev_dbg(pcdev->dev, "%s: Releasing camera reset\n",
  523. __func__);
  524. pdata->reset(pcdev->dev, 1);
  525. }
  526. CICR0 = 0x3FF; /* disable all interrupts */
  527. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  528. cicr4 |= CICR4_PCLK_EN;
  529. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  530. cicr4 |= CICR4_MCLK_EN;
  531. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  532. cicr4 |= CICR4_PCP;
  533. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  534. cicr4 |= CICR4_HSP;
  535. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  536. cicr4 |= CICR4_VSP;
  537. CICR4 = mclk_get_divisor(pcdev) | cicr4;
  538. clk_enable(pcdev->clk);
  539. }
  540. static void pxa_camera_deactivate(struct pxa_camera_dev *pcdev)
  541. {
  542. struct pxacamera_platform_data *board = pcdev->pdata;
  543. clk_disable(pcdev->clk);
  544. if (board && board->reset) {
  545. dev_dbg(pcdev->dev, "%s: Asserting camera reset\n",
  546. __func__);
  547. board->reset(pcdev->dev, 0);
  548. }
  549. if (board && board->power) {
  550. dev_dbg(pcdev->dev, "%s: Power off camera\n", __func__);
  551. board->power(pcdev->dev, 0);
  552. }
  553. }
  554. static irqreturn_t pxa_camera_irq(int irq, void *data)
  555. {
  556. struct pxa_camera_dev *pcdev = data;
  557. unsigned int status = CISR;
  558. dev_dbg(pcdev->dev, "Camera interrupt status 0x%x\n", status);
  559. if (!status)
  560. return IRQ_NONE;
  561. CISR = status;
  562. if (status & CISR_EOF) {
  563. int i;
  564. for (i = 0; i < pcdev->channels; i++) {
  565. DDADR(pcdev->dma_chans[i]) =
  566. pcdev->active->dmas[i].sg_dma;
  567. DCSR(pcdev->dma_chans[i]) = DCSR_RUN;
  568. }
  569. CICR0 |= CICR0_EOFM;
  570. }
  571. return IRQ_HANDLED;
  572. }
  573. /* The following two functions absolutely depend on the fact, that
  574. * there can be only one camera on PXA quick capture interface */
  575. static int pxa_camera_add_device(struct soc_camera_device *icd)
  576. {
  577. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  578. struct pxa_camera_dev *pcdev = ici->priv;
  579. int ret;
  580. mutex_lock(&camera_lock);
  581. if (pcdev->icd) {
  582. ret = -EBUSY;
  583. goto ebusy;
  584. }
  585. dev_info(&icd->dev, "PXA Camera driver attached to camera %d\n",
  586. icd->devnum);
  587. pxa_camera_activate(pcdev);
  588. ret = icd->ops->init(icd);
  589. if (!ret)
  590. pcdev->icd = icd;
  591. ebusy:
  592. mutex_unlock(&camera_lock);
  593. return ret;
  594. }
  595. static void pxa_camera_remove_device(struct soc_camera_device *icd)
  596. {
  597. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  598. struct pxa_camera_dev *pcdev = ici->priv;
  599. BUG_ON(icd != pcdev->icd);
  600. dev_info(&icd->dev, "PXA Camera driver detached from camera %d\n",
  601. icd->devnum);
  602. /* disable capture, disable interrupts */
  603. CICR0 = 0x3ff;
  604. /* Stop DMA engine */
  605. DCSR(pcdev->dma_chans[0]) = 0;
  606. DCSR(pcdev->dma_chans[1]) = 0;
  607. DCSR(pcdev->dma_chans[2]) = 0;
  608. icd->ops->release(icd);
  609. pxa_camera_deactivate(pcdev);
  610. pcdev->icd = NULL;
  611. }
  612. static int test_platform_param(struct pxa_camera_dev *pcdev,
  613. unsigned char buswidth, unsigned long *flags)
  614. {
  615. /*
  616. * Platform specified synchronization and pixel clock polarities are
  617. * only a recommendation and are only used during probing. The PXA270
  618. * quick capture interface supports both.
  619. */
  620. *flags = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  621. SOCAM_MASTER : SOCAM_SLAVE) |
  622. SOCAM_HSYNC_ACTIVE_HIGH |
  623. SOCAM_HSYNC_ACTIVE_LOW |
  624. SOCAM_VSYNC_ACTIVE_HIGH |
  625. SOCAM_VSYNC_ACTIVE_LOW |
  626. SOCAM_PCLK_SAMPLE_RISING |
  627. SOCAM_PCLK_SAMPLE_FALLING;
  628. /* If requested data width is supported by the platform, use it */
  629. switch (buswidth) {
  630. case 10:
  631. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_10))
  632. return -EINVAL;
  633. *flags |= SOCAM_DATAWIDTH_10;
  634. break;
  635. case 9:
  636. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_9))
  637. return -EINVAL;
  638. *flags |= SOCAM_DATAWIDTH_9;
  639. break;
  640. case 8:
  641. if (!(pcdev->platform_flags & PXA_CAMERA_DATAWIDTH_8))
  642. return -EINVAL;
  643. *flags |= SOCAM_DATAWIDTH_8;
  644. }
  645. return 0;
  646. }
  647. static int pxa_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  648. {
  649. struct soc_camera_host *ici =
  650. to_soc_camera_host(icd->dev.parent);
  651. struct pxa_camera_dev *pcdev = ici->priv;
  652. unsigned long dw, bpp, bus_flags, camera_flags, common_flags;
  653. u32 cicr0, cicr1, cicr4 = 0;
  654. int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
  655. if (ret < 0)
  656. return ret;
  657. camera_flags = icd->ops->query_bus_param(icd);
  658. common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
  659. if (!common_flags)
  660. return -EINVAL;
  661. pcdev->channels = 1;
  662. /* Make choises, based on platform preferences */
  663. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  664. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  665. if (pcdev->platform_flags & PXA_CAMERA_HSP)
  666. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  667. else
  668. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  669. }
  670. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  671. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  672. if (pcdev->platform_flags & PXA_CAMERA_VSP)
  673. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  674. else
  675. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  676. }
  677. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  678. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  679. if (pcdev->platform_flags & PXA_CAMERA_PCP)
  680. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  681. else
  682. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  683. }
  684. ret = icd->ops->set_bus_param(icd, common_flags);
  685. if (ret < 0)
  686. return ret;
  687. /* Datawidth is now guaranteed to be equal to one of the three values.
  688. * We fix bit-per-pixel equal to data-width... */
  689. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  690. case SOCAM_DATAWIDTH_10:
  691. icd->buswidth = 10;
  692. dw = 4;
  693. bpp = 0x40;
  694. break;
  695. case SOCAM_DATAWIDTH_9:
  696. icd->buswidth = 9;
  697. dw = 3;
  698. bpp = 0x20;
  699. break;
  700. default:
  701. /* Actually it can only be 8 now,
  702. * default is just to silence compiler warnings */
  703. case SOCAM_DATAWIDTH_8:
  704. icd->buswidth = 8;
  705. dw = 2;
  706. bpp = 0;
  707. }
  708. if (pcdev->platform_flags & PXA_CAMERA_PCLK_EN)
  709. cicr4 |= CICR4_PCLK_EN;
  710. if (pcdev->platform_flags & PXA_CAMERA_MCLK_EN)
  711. cicr4 |= CICR4_MCLK_EN;
  712. if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
  713. cicr4 |= CICR4_PCP;
  714. if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
  715. cicr4 |= CICR4_HSP;
  716. if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
  717. cicr4 |= CICR4_VSP;
  718. cicr0 = CICR0;
  719. if (cicr0 & CICR0_ENB)
  720. CICR0 = cicr0 & ~CICR0_ENB;
  721. cicr1 = CICR1_PPL_VAL(icd->width - 1) | bpp | dw;
  722. switch (pixfmt) {
  723. case V4L2_PIX_FMT_YUV422P:
  724. pcdev->channels = 3;
  725. cicr1 |= CICR1_YCBCR_F;
  726. case V4L2_PIX_FMT_YUYV:
  727. cicr1 |= CICR1_COLOR_SP_VAL(2);
  728. break;
  729. case V4L2_PIX_FMT_RGB555:
  730. cicr1 |= CICR1_RGB_BPP_VAL(1) | CICR1_RGBT_CONV_VAL(2) |
  731. CICR1_TBIT | CICR1_COLOR_SP_VAL(1);
  732. break;
  733. case V4L2_PIX_FMT_RGB565:
  734. cicr1 |= CICR1_COLOR_SP_VAL(1) | CICR1_RGB_BPP_VAL(2);
  735. break;
  736. }
  737. CICR1 = cicr1;
  738. CICR2 = 0;
  739. CICR3 = CICR3_LPF_VAL(icd->height - 1) |
  740. CICR3_BFW_VAL(min((unsigned short)255, icd->y_skip_top));
  741. CICR4 = mclk_get_divisor(pcdev) | cicr4;
  742. /* CIF interrupts are not used, only DMA */
  743. CICR0 = (pcdev->platform_flags & PXA_CAMERA_MASTER ?
  744. CICR0_SIM_MP : (CICR0_SL_CAP_EN | CICR0_SIM_SP)) |
  745. CICR0_DMAEN | CICR0_IRQ_MASK | (cicr0 & CICR0_ENB);
  746. return 0;
  747. }
  748. static int pxa_camera_try_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  749. {
  750. struct soc_camera_host *ici =
  751. to_soc_camera_host(icd->dev.parent);
  752. struct pxa_camera_dev *pcdev = ici->priv;
  753. unsigned long bus_flags, camera_flags;
  754. int ret = test_platform_param(pcdev, icd->buswidth, &bus_flags);
  755. if (ret < 0)
  756. return ret;
  757. camera_flags = icd->ops->query_bus_param(icd);
  758. return soc_camera_bus_param_compatible(camera_flags, bus_flags) ? 0 : -EINVAL;
  759. }
  760. static int pxa_camera_set_fmt_cap(struct soc_camera_device *icd,
  761. __u32 pixfmt, struct v4l2_rect *rect)
  762. {
  763. return icd->ops->set_fmt_cap(icd, pixfmt, rect);
  764. }
  765. static int pxa_camera_try_fmt_cap(struct soc_camera_device *icd,
  766. struct v4l2_format *f)
  767. {
  768. /* limit to pxa hardware capabilities */
  769. if (f->fmt.pix.height < 32)
  770. f->fmt.pix.height = 32;
  771. if (f->fmt.pix.height > 2048)
  772. f->fmt.pix.height = 2048;
  773. if (f->fmt.pix.width < 48)
  774. f->fmt.pix.width = 48;
  775. if (f->fmt.pix.width > 2048)
  776. f->fmt.pix.width = 2048;
  777. f->fmt.pix.width &= ~0x01;
  778. /* limit to sensor capabilities */
  779. return icd->ops->try_fmt_cap(icd, f);
  780. }
  781. static int pxa_camera_reqbufs(struct soc_camera_file *icf,
  782. struct v4l2_requestbuffers *p)
  783. {
  784. int i;
  785. /* This is for locking debugging only. I removed spinlocks and now I
  786. * check whether .prepare is ever called on a linked buffer, or whether
  787. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  788. * it hadn't triggered */
  789. for (i = 0; i < p->count; i++) {
  790. struct pxa_buffer *buf = container_of(icf->vb_vidq.bufs[i],
  791. struct pxa_buffer, vb);
  792. buf->inwork = 0;
  793. INIT_LIST_HEAD(&buf->vb.queue);
  794. }
  795. return 0;
  796. }
  797. static unsigned int pxa_camera_poll(struct file *file, poll_table *pt)
  798. {
  799. struct soc_camera_file *icf = file->private_data;
  800. struct pxa_buffer *buf;
  801. buf = list_entry(icf->vb_vidq.stream.next, struct pxa_buffer,
  802. vb.stream);
  803. poll_wait(file, &buf->vb.done, pt);
  804. if (buf->vb.state == VIDEOBUF_DONE ||
  805. buf->vb.state == VIDEOBUF_ERROR)
  806. return POLLIN|POLLRDNORM;
  807. return 0;
  808. }
  809. static int pxa_camera_querycap(struct soc_camera_host *ici,
  810. struct v4l2_capability *cap)
  811. {
  812. /* cap->name is set by the firendly caller:-> */
  813. strlcpy(cap->card, pxa_cam_driver_description, sizeof(cap->card));
  814. cap->version = PXA_CAM_VERSION_CODE;
  815. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  816. return 0;
  817. }
  818. static int pxa_camera_suspend(struct soc_camera_device *icd, pm_message_t state)
  819. {
  820. struct soc_camera_host *ici =
  821. to_soc_camera_host(icd->dev.parent);
  822. struct pxa_camera_dev *pcdev = ici->priv;
  823. int i = 0, ret = 0;
  824. pcdev->save_cicr[i++] = CICR0;
  825. pcdev->save_cicr[i++] = CICR1;
  826. pcdev->save_cicr[i++] = CICR2;
  827. pcdev->save_cicr[i++] = CICR3;
  828. pcdev->save_cicr[i++] = CICR4;
  829. if ((pcdev->icd) && (pcdev->icd->ops->suspend))
  830. ret = pcdev->icd->ops->suspend(pcdev->icd, state);
  831. return ret;
  832. }
  833. static int pxa_camera_resume(struct soc_camera_device *icd)
  834. {
  835. struct soc_camera_host *ici =
  836. to_soc_camera_host(icd->dev.parent);
  837. struct pxa_camera_dev *pcdev = ici->priv;
  838. int i = 0, ret = 0;
  839. DRCMR68 = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  840. DRCMR69 = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  841. DRCMR70 = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  842. CICR0 = pcdev->save_cicr[i++] & ~CICR0_ENB;
  843. CICR1 = pcdev->save_cicr[i++];
  844. CICR2 = pcdev->save_cicr[i++];
  845. CICR3 = pcdev->save_cicr[i++];
  846. CICR4 = pcdev->save_cicr[i++];
  847. if ((pcdev->icd) && (pcdev->icd->ops->resume))
  848. ret = pcdev->icd->ops->resume(pcdev->icd);
  849. /* Restart frame capture if active buffer exists */
  850. if (!ret && pcdev->active) {
  851. /* Reset the FIFOs */
  852. CIFR |= CIFR_RESET_F;
  853. /* Enable End-Of-Frame Interrupt */
  854. CICR0 &= ~CICR0_EOFM;
  855. /* Restart the Capture Interface */
  856. CICR0 |= CICR0_ENB;
  857. }
  858. return ret;
  859. }
  860. static struct soc_camera_host_ops pxa_soc_camera_host_ops = {
  861. .owner = THIS_MODULE,
  862. .add = pxa_camera_add_device,
  863. .remove = pxa_camera_remove_device,
  864. .suspend = pxa_camera_suspend,
  865. .resume = pxa_camera_resume,
  866. .set_fmt_cap = pxa_camera_set_fmt_cap,
  867. .try_fmt_cap = pxa_camera_try_fmt_cap,
  868. .init_videobuf = pxa_camera_init_videobuf,
  869. .reqbufs = pxa_camera_reqbufs,
  870. .poll = pxa_camera_poll,
  871. .querycap = pxa_camera_querycap,
  872. .try_bus_param = pxa_camera_try_bus_param,
  873. .set_bus_param = pxa_camera_set_bus_param,
  874. };
  875. /* Should be allocated dynamically too, but we have only one. */
  876. static struct soc_camera_host pxa_soc_camera_host = {
  877. .drv_name = PXA_CAM_DRV_NAME,
  878. .ops = &pxa_soc_camera_host_ops,
  879. };
  880. static int pxa_camera_probe(struct platform_device *pdev)
  881. {
  882. struct pxa_camera_dev *pcdev;
  883. struct resource *res;
  884. void __iomem *base;
  885. int irq;
  886. int err = 0;
  887. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  888. irq = platform_get_irq(pdev, 0);
  889. if (!res || irq < 0) {
  890. err = -ENODEV;
  891. goto exit;
  892. }
  893. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  894. if (!pcdev) {
  895. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  896. err = -ENOMEM;
  897. goto exit;
  898. }
  899. pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
  900. if (IS_ERR(pcdev->clk)) {
  901. err = PTR_ERR(pcdev->clk);
  902. goto exit_kfree;
  903. }
  904. dev_set_drvdata(&pdev->dev, pcdev);
  905. pcdev->res = res;
  906. pcdev->pdata = pdev->dev.platform_data;
  907. pcdev->platform_flags = pcdev->pdata->flags;
  908. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  909. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  910. /* Platform hasn't set available data widths. This is bad.
  911. * Warn and use a default. */
  912. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  913. "data widths, using default 10 bit\n");
  914. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  915. }
  916. pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
  917. if (!pcdev->platform_mclk_10khz) {
  918. dev_warn(&pdev->dev,
  919. "mclk_10khz == 0! Please, fix your platform data. "
  920. "Using default 20MHz\n");
  921. pcdev->platform_mclk_10khz = 2000;
  922. }
  923. INIT_LIST_HEAD(&pcdev->capture);
  924. spin_lock_init(&pcdev->lock);
  925. /*
  926. * Request the regions.
  927. */
  928. if (!request_mem_region(res->start, res->end - res->start + 1,
  929. PXA_CAM_DRV_NAME)) {
  930. err = -EBUSY;
  931. goto exit_clk;
  932. }
  933. base = ioremap(res->start, res->end - res->start + 1);
  934. if (!base) {
  935. err = -ENOMEM;
  936. goto exit_release;
  937. }
  938. pcdev->irq = irq;
  939. pcdev->base = base;
  940. pcdev->dev = &pdev->dev;
  941. /* request dma */
  942. pcdev->dma_chans[0] = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  943. pxa_camera_dma_irq_y, pcdev);
  944. if (pcdev->dma_chans[0] < 0) {
  945. dev_err(pcdev->dev, "Can't request DMA for Y\n");
  946. err = -ENOMEM;
  947. goto exit_iounmap;
  948. }
  949. dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chans[0]);
  950. pcdev->dma_chans[1] = pxa_request_dma("CI_U", DMA_PRIO_HIGH,
  951. pxa_camera_dma_irq_u, pcdev);
  952. if (pcdev->dma_chans[1] < 0) {
  953. dev_err(pcdev->dev, "Can't request DMA for U\n");
  954. err = -ENOMEM;
  955. goto exit_free_dma_y;
  956. }
  957. dev_dbg(pcdev->dev, "got DMA channel (U) %d\n", pcdev->dma_chans[1]);
  958. pcdev->dma_chans[2] = pxa_request_dma("CI_V", DMA_PRIO_HIGH,
  959. pxa_camera_dma_irq_v, pcdev);
  960. if (pcdev->dma_chans[0] < 0) {
  961. dev_err(pcdev->dev, "Can't request DMA for V\n");
  962. err = -ENOMEM;
  963. goto exit_free_dma_u;
  964. }
  965. dev_dbg(pcdev->dev, "got DMA channel (V) %d\n", pcdev->dma_chans[2]);
  966. DRCMR68 = pcdev->dma_chans[0] | DRCMR_MAPVLD;
  967. DRCMR69 = pcdev->dma_chans[1] | DRCMR_MAPVLD;
  968. DRCMR70 = pcdev->dma_chans[2] | DRCMR_MAPVLD;
  969. /* request irq */
  970. err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
  971. pcdev);
  972. if (err) {
  973. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  974. goto exit_free_dma;
  975. }
  976. pxa_soc_camera_host.priv = pcdev;
  977. pxa_soc_camera_host.dev.parent = &pdev->dev;
  978. pxa_soc_camera_host.nr = pdev->id;
  979. err = soc_camera_host_register(&pxa_soc_camera_host);
  980. if (err)
  981. goto exit_free_irq;
  982. return 0;
  983. exit_free_irq:
  984. free_irq(pcdev->irq, pcdev);
  985. exit_free_dma:
  986. pxa_free_dma(pcdev->dma_chans[2]);
  987. exit_free_dma_u:
  988. pxa_free_dma(pcdev->dma_chans[1]);
  989. exit_free_dma_y:
  990. pxa_free_dma(pcdev->dma_chans[0]);
  991. exit_iounmap:
  992. iounmap(base);
  993. exit_release:
  994. release_mem_region(res->start, res->end - res->start + 1);
  995. exit_clk:
  996. clk_put(pcdev->clk);
  997. exit_kfree:
  998. kfree(pcdev);
  999. exit:
  1000. return err;
  1001. }
  1002. static int __devexit pxa_camera_remove(struct platform_device *pdev)
  1003. {
  1004. struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
  1005. struct resource *res;
  1006. clk_put(pcdev->clk);
  1007. pxa_free_dma(pcdev->dma_chans[0]);
  1008. pxa_free_dma(pcdev->dma_chans[1]);
  1009. pxa_free_dma(pcdev->dma_chans[2]);
  1010. free_irq(pcdev->irq, pcdev);
  1011. soc_camera_host_unregister(&pxa_soc_camera_host);
  1012. iounmap(pcdev->base);
  1013. res = pcdev->res;
  1014. release_mem_region(res->start, res->end - res->start + 1);
  1015. kfree(pcdev);
  1016. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  1017. return 0;
  1018. }
  1019. static struct platform_driver pxa_camera_driver = {
  1020. .driver = {
  1021. .name = PXA_CAM_DRV_NAME,
  1022. },
  1023. .probe = pxa_camera_probe,
  1024. .remove = __exit_p(pxa_camera_remove),
  1025. };
  1026. static int __devinit pxa_camera_init(void)
  1027. {
  1028. return platform_driver_register(&pxa_camera_driver);
  1029. }
  1030. static void __exit pxa_camera_exit(void)
  1031. {
  1032. platform_driver_unregister(&pxa_camera_driver);
  1033. }
  1034. module_init(pxa_camera_init);
  1035. module_exit(pxa_camera_exit);
  1036. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  1037. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  1038. MODULE_LICENSE("GPL");