pxa_camera.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. /* Should beallocated dynamically too, but we have only one. */
  654. static struct soc_camera_host pxa_soc_camera_host = {
  655. .drv_name = PXA_CAM_DRV_NAME,
  656. .vbq_ops = &pxa_videobuf_ops,
  657. .add = pxa_camera_add_device,
  658. .remove = pxa_camera_remove_device,
  659. .msize = sizeof(struct pxa_buffer),
  660. .set_fmt_cap = pxa_camera_set_fmt_cap,
  661. .try_fmt_cap = pxa_camera_try_fmt_cap,
  662. .reqbufs = pxa_camera_reqbufs,
  663. .poll = pxa_camera_poll,
  664. .querycap = pxa_camera_querycap,
  665. .try_bus_param = pxa_camera_try_bus_param,
  666. .set_bus_param = pxa_camera_set_bus_param,
  667. };
  668. static int pxa_camera_probe(struct platform_device *pdev)
  669. {
  670. struct pxa_camera_dev *pcdev;
  671. struct resource *res;
  672. void __iomem *base;
  673. unsigned int irq;
  674. int err = 0;
  675. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  676. irq = platform_get_irq(pdev, 0);
  677. if (!res || !irq) {
  678. err = -ENODEV;
  679. goto exit;
  680. }
  681. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  682. if (!pcdev) {
  683. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  684. err = -ENOMEM;
  685. goto exit;
  686. }
  687. pcdev->clk = clk_get(&pdev->dev, "CAMCLK");
  688. if (IS_ERR(pcdev->clk)) {
  689. err = PTR_ERR(pcdev->clk);
  690. goto exit_kfree;
  691. }
  692. dev_set_drvdata(&pdev->dev, pcdev);
  693. pcdev->res = res;
  694. pcdev->pdata = pdev->dev.platform_data;
  695. pcdev->platform_flags = pcdev->pdata->flags;
  696. if (!(pcdev->platform_flags & (PXA_CAMERA_DATAWIDTH_8 |
  697. PXA_CAMERA_DATAWIDTH_9 | PXA_CAMERA_DATAWIDTH_10))) {
  698. /* Platform hasn't set available data widths. This is bad.
  699. * Warn and use a default. */
  700. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  701. "data widths, using default 10 bit\n");
  702. pcdev->platform_flags |= PXA_CAMERA_DATAWIDTH_10;
  703. }
  704. pcdev->platform_mclk_10khz = pcdev->pdata->mclk_10khz;
  705. if (!pcdev->platform_mclk_10khz) {
  706. dev_warn(&pdev->dev,
  707. "mclk_10khz == 0! Please, fix your platform data. "
  708. "Using default 20MHz\n");
  709. pcdev->platform_mclk_10khz = 2000;
  710. }
  711. INIT_LIST_HEAD(&pcdev->capture);
  712. spin_lock_init(&pcdev->lock);
  713. /*
  714. * Request the regions.
  715. */
  716. if (!request_mem_region(res->start, res->end - res->start + 1,
  717. PXA_CAM_DRV_NAME)) {
  718. err = -EBUSY;
  719. goto exit_clk;
  720. }
  721. base = ioremap(res->start, res->end - res->start + 1);
  722. if (!base) {
  723. err = -ENOMEM;
  724. goto exit_release;
  725. }
  726. pcdev->irq = irq;
  727. pcdev->base = base;
  728. pcdev->dev = &pdev->dev;
  729. /* request dma */
  730. pcdev->dma_chan_y = pxa_request_dma("CI_Y", DMA_PRIO_HIGH,
  731. pxa_camera_dma_irq_y, pcdev);
  732. if (pcdev->dma_chan_y < 0) {
  733. dev_err(pcdev->dev, "Can't request DMA for Y\n");
  734. err = -ENOMEM;
  735. goto exit_iounmap;
  736. }
  737. dev_dbg(pcdev->dev, "got DMA channel %d\n", pcdev->dma_chan_y);
  738. DRCMR68 = pcdev->dma_chan_y | DRCMR_MAPVLD;
  739. /* request irq */
  740. err = request_irq(pcdev->irq, pxa_camera_irq, 0, PXA_CAM_DRV_NAME,
  741. pcdev);
  742. if (err) {
  743. dev_err(pcdev->dev, "Camera interrupt register failed \n");
  744. goto exit_free_dma;
  745. }
  746. pxa_soc_camera_host.priv = pcdev;
  747. pxa_soc_camera_host.dev.parent = &pdev->dev;
  748. pxa_soc_camera_host.nr = pdev->id;
  749. err = soc_camera_host_register(&pxa_soc_camera_host, THIS_MODULE);
  750. if (err)
  751. goto exit_free_irq;
  752. return 0;
  753. exit_free_irq:
  754. free_irq(pcdev->irq, pcdev);
  755. exit_free_dma:
  756. pxa_free_dma(pcdev->dma_chan_y);
  757. exit_iounmap:
  758. iounmap(base);
  759. exit_release:
  760. release_mem_region(res->start, res->end - res->start + 1);
  761. exit_clk:
  762. clk_put(pcdev->clk);
  763. exit_kfree:
  764. kfree(pcdev);
  765. exit:
  766. return err;
  767. }
  768. static int __devexit pxa_camera_remove(struct platform_device *pdev)
  769. {
  770. struct pxa_camera_dev *pcdev = platform_get_drvdata(pdev);
  771. struct resource *res;
  772. clk_put(pcdev->clk);
  773. pxa_free_dma(pcdev->dma_chan_y);
  774. free_irq(pcdev->irq, pcdev);
  775. soc_camera_host_unregister(&pxa_soc_camera_host);
  776. iounmap(pcdev->base);
  777. res = pcdev->res;
  778. release_mem_region(res->start, res->end - res->start + 1);
  779. kfree(pcdev);
  780. dev_info(&pdev->dev, "PXA Camera driver unloaded\n");
  781. return 0;
  782. }
  783. static struct platform_driver pxa_camera_driver = {
  784. .driver = {
  785. .name = PXA_CAM_DRV_NAME,
  786. },
  787. .probe = pxa_camera_probe,
  788. .remove = __exit_p(pxa_camera_remove),
  789. };
  790. static int __devinit pxa_camera_init(void)
  791. {
  792. return platform_driver_register(&pxa_camera_driver);
  793. }
  794. static void __exit pxa_camera_exit(void)
  795. {
  796. return platform_driver_unregister(&pxa_camera_driver);
  797. }
  798. module_init(pxa_camera_init);
  799. module_exit(pxa_camera_exit);
  800. MODULE_DESCRIPTION("PXA27x SoC Camera Host driver");
  801. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  802. MODULE_LICENSE("GPL");