omap24xxcam.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  1. /*
  2. * drivers/media/video/omap24xxcam.c
  3. *
  4. * OMAP 2 camera block driver.
  5. *
  6. * Copyright (C) 2004 MontaVista Software, Inc.
  7. * Copyright (C) 2004 Texas Instruments.
  8. * Copyright (C) 2007-2008 Nokia Corporation.
  9. *
  10. * Contact: Sakari Ailus <sakari.ailus@nokia.com>
  11. *
  12. * Based on code from Andy Lowe <source@mvista.com>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * version 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  26. * 02110-1301 USA
  27. */
  28. #include <linux/delay.h>
  29. #include <linux/kernel.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/pci.h> /* needed for videobufs */
  33. #include <linux/version.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/clk.h>
  36. #include <linux/io.h>
  37. #include <linux/slab.h>
  38. #include <linux/sched.h>
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ioctl.h>
  41. #include "omap24xxcam.h"
  42. #define OMAP24XXCAM_VERSION KERNEL_VERSION(0, 0, 0)
  43. #define RESET_TIMEOUT_NS 10000
  44. static void omap24xxcam_reset(struct omap24xxcam_device *cam);
  45. static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam);
  46. static void omap24xxcam_device_unregister(struct v4l2_int_device *s);
  47. static int omap24xxcam_remove(struct platform_device *pdev);
  48. /* module parameters */
  49. static int video_nr = -1; /* video device minor (-1 ==> auto assign) */
  50. /*
  51. * Maximum amount of memory to use for capture buffers.
  52. * Default is 4800KB, enough to double-buffer SXGA.
  53. */
  54. static int capture_mem = 1280 * 960 * 2 * 2;
  55. static struct v4l2_int_device omap24xxcam;
  56. /*
  57. *
  58. * Clocks.
  59. *
  60. */
  61. static void omap24xxcam_clock_put(struct omap24xxcam_device *cam)
  62. {
  63. if (cam->ick != NULL && !IS_ERR(cam->ick))
  64. clk_put(cam->ick);
  65. if (cam->fck != NULL && !IS_ERR(cam->fck))
  66. clk_put(cam->fck);
  67. cam->ick = cam->fck = NULL;
  68. }
  69. static int omap24xxcam_clock_get(struct omap24xxcam_device *cam)
  70. {
  71. int rval = 0;
  72. cam->fck = clk_get(cam->dev, "fck");
  73. if (IS_ERR(cam->fck)) {
  74. dev_err(cam->dev, "can't get camera fck");
  75. rval = PTR_ERR(cam->fck);
  76. omap24xxcam_clock_put(cam);
  77. return rval;
  78. }
  79. cam->ick = clk_get(cam->dev, "ick");
  80. if (IS_ERR(cam->ick)) {
  81. dev_err(cam->dev, "can't get camera ick");
  82. rval = PTR_ERR(cam->ick);
  83. omap24xxcam_clock_put(cam);
  84. }
  85. return rval;
  86. }
  87. static void omap24xxcam_clock_on(struct omap24xxcam_device *cam)
  88. {
  89. clk_enable(cam->fck);
  90. clk_enable(cam->ick);
  91. }
  92. static void omap24xxcam_clock_off(struct omap24xxcam_device *cam)
  93. {
  94. clk_disable(cam->fck);
  95. clk_disable(cam->ick);
  96. }
  97. /*
  98. *
  99. * Camera core
  100. *
  101. */
  102. /*
  103. * Set xclk.
  104. *
  105. * To disable xclk, use value zero.
  106. */
  107. static void omap24xxcam_core_xclk_set(const struct omap24xxcam_device *cam,
  108. u32 xclk)
  109. {
  110. if (xclk) {
  111. u32 divisor = CAM_MCLK / xclk;
  112. if (divisor == 1)
  113. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
  114. CC_CTRL_XCLK,
  115. CC_CTRL_XCLK_DIV_BYPASS);
  116. else
  117. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
  118. CC_CTRL_XCLK, divisor);
  119. } else
  120. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET,
  121. CC_CTRL_XCLK, CC_CTRL_XCLK_DIV_STABLE_LOW);
  122. }
  123. static void omap24xxcam_core_hwinit(const struct omap24xxcam_device *cam)
  124. {
  125. /*
  126. * Setting the camera core AUTOIDLE bit causes problems with frame
  127. * synchronization, so we will clear the AUTOIDLE bit instead.
  128. */
  129. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_SYSCONFIG,
  130. CC_SYSCONFIG_AUTOIDLE);
  131. /* program the camera interface DMA packet size */
  132. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL_DMA,
  133. CC_CTRL_DMA_EN | (DMA_THRESHOLD / 4 - 1));
  134. /* enable camera core error interrupts */
  135. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQENABLE,
  136. CC_IRQENABLE_FW_ERR_IRQ
  137. | CC_IRQENABLE_FSC_ERR_IRQ
  138. | CC_IRQENABLE_SSC_ERR_IRQ
  139. | CC_IRQENABLE_FIFO_OF_IRQ);
  140. }
  141. /*
  142. * Enable the camera core.
  143. *
  144. * Data transfer to the camera DMA starts from next starting frame.
  145. */
  146. static void omap24xxcam_core_enable(const struct omap24xxcam_device *cam)
  147. {
  148. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
  149. cam->cc_ctrl);
  150. }
  151. /*
  152. * Disable camera core.
  153. *
  154. * The data transfer will be stopped immediately (CC_CTRL_CC_RST). The
  155. * core internal state machines will be reset. Use
  156. * CC_CTRL_CC_FRAME_TRIG instead if you want to transfer the current
  157. * frame completely.
  158. */
  159. static void omap24xxcam_core_disable(const struct omap24xxcam_device *cam)
  160. {
  161. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_CTRL,
  162. CC_CTRL_CC_RST);
  163. }
  164. /* Interrupt service routine for camera core interrupts. */
  165. static void omap24xxcam_core_isr(struct omap24xxcam_device *cam)
  166. {
  167. u32 cc_irqstatus;
  168. const u32 cc_irqstatus_err =
  169. CC_IRQSTATUS_FW_ERR_IRQ
  170. | CC_IRQSTATUS_FSC_ERR_IRQ
  171. | CC_IRQSTATUS_SSC_ERR_IRQ
  172. | CC_IRQSTATUS_FIFO_UF_IRQ
  173. | CC_IRQSTATUS_FIFO_OF_IRQ;
  174. cc_irqstatus = omap24xxcam_reg_in(cam->mmio_base + CC_REG_OFFSET,
  175. CC_IRQSTATUS);
  176. omap24xxcam_reg_out(cam->mmio_base + CC_REG_OFFSET, CC_IRQSTATUS,
  177. cc_irqstatus);
  178. if (cc_irqstatus & cc_irqstatus_err
  179. && !atomic_read(&cam->in_reset)) {
  180. dev_dbg(cam->dev, "resetting camera, cc_irqstatus 0x%x\n",
  181. cc_irqstatus);
  182. omap24xxcam_reset(cam);
  183. }
  184. }
  185. /*
  186. *
  187. * videobuf_buffer handling.
  188. *
  189. * Memory for mmapped videobuf_buffers is not allocated
  190. * conventionally, but by several kmalloc allocations and then
  191. * creating the scatterlist on our own. User-space buffers are handled
  192. * normally.
  193. *
  194. */
  195. /*
  196. * Free the memory-mapped buffer memory allocated for a
  197. * videobuf_buffer and the associated scatterlist.
  198. */
  199. static void omap24xxcam_vbq_free_mmap_buffer(struct videobuf_buffer *vb)
  200. {
  201. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  202. size_t alloc_size;
  203. struct page *page;
  204. int i;
  205. if (dma->sglist == NULL)
  206. return;
  207. i = dma->sglen;
  208. while (i) {
  209. i--;
  210. alloc_size = sg_dma_len(&dma->sglist[i]);
  211. page = sg_page(&dma->sglist[i]);
  212. do {
  213. ClearPageReserved(page++);
  214. } while (alloc_size -= PAGE_SIZE);
  215. __free_pages(sg_page(&dma->sglist[i]),
  216. get_order(sg_dma_len(&dma->sglist[i])));
  217. }
  218. kfree(dma->sglist);
  219. dma->sglist = NULL;
  220. }
  221. /* Release all memory related to the videobuf_queue. */
  222. static void omap24xxcam_vbq_free_mmap_buffers(struct videobuf_queue *vbq)
  223. {
  224. int i;
  225. mutex_lock(&vbq->vb_lock);
  226. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  227. if (NULL == vbq->bufs[i])
  228. continue;
  229. if (V4L2_MEMORY_MMAP != vbq->bufs[i]->memory)
  230. continue;
  231. vbq->ops->buf_release(vbq, vbq->bufs[i]);
  232. omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
  233. kfree(vbq->bufs[i]);
  234. vbq->bufs[i] = NULL;
  235. }
  236. mutex_unlock(&vbq->vb_lock);
  237. videobuf_mmap_free(vbq);
  238. }
  239. /*
  240. * Allocate physically as contiguous as possible buffer for video
  241. * frame and allocate and build DMA scatter-gather list for it.
  242. */
  243. static int omap24xxcam_vbq_alloc_mmap_buffer(struct videobuf_buffer *vb)
  244. {
  245. unsigned int order;
  246. size_t alloc_size, size = vb->bsize; /* vb->bsize is page aligned */
  247. struct page *page;
  248. int max_pages, err = 0, i = 0;
  249. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  250. /*
  251. * allocate maximum size scatter-gather list. Note this is
  252. * overhead. We may not use as many entries as we allocate
  253. */
  254. max_pages = vb->bsize >> PAGE_SHIFT;
  255. dma->sglist = kcalloc(max_pages, sizeof(*dma->sglist), GFP_KERNEL);
  256. if (dma->sglist == NULL) {
  257. err = -ENOMEM;
  258. goto out;
  259. }
  260. while (size) {
  261. order = get_order(size);
  262. /*
  263. * do not over-allocate even if we would get larger
  264. * contiguous chunk that way
  265. */
  266. if ((PAGE_SIZE << order) > size)
  267. order--;
  268. /* try to allocate as many contiguous pages as possible */
  269. page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
  270. /* if allocation fails, try to allocate smaller amount */
  271. while (page == NULL) {
  272. order--;
  273. page = alloc_pages(GFP_KERNEL | GFP_DMA, order);
  274. if (page == NULL && !order) {
  275. err = -ENOMEM;
  276. goto out;
  277. }
  278. }
  279. size -= (PAGE_SIZE << order);
  280. /* append allocated chunk of pages into scatter-gather list */
  281. sg_set_page(&dma->sglist[i], page, PAGE_SIZE << order, 0);
  282. dma->sglen++;
  283. i++;
  284. alloc_size = (PAGE_SIZE << order);
  285. /* clear pages before giving them to user space */
  286. memset(page_address(page), 0, alloc_size);
  287. /* mark allocated pages reserved */
  288. do {
  289. SetPageReserved(page++);
  290. } while (alloc_size -= PAGE_SIZE);
  291. }
  292. /*
  293. * REVISIT: not fully correct to assign nr_pages == sglen but
  294. * video-buf is passing nr_pages for e.g. unmap_sg calls
  295. */
  296. dma->nr_pages = dma->sglen;
  297. dma->direction = PCI_DMA_FROMDEVICE;
  298. return 0;
  299. out:
  300. omap24xxcam_vbq_free_mmap_buffer(vb);
  301. return err;
  302. }
  303. static int omap24xxcam_vbq_alloc_mmap_buffers(struct videobuf_queue *vbq,
  304. unsigned int count)
  305. {
  306. int i, err = 0;
  307. struct omap24xxcam_fh *fh =
  308. container_of(vbq, struct omap24xxcam_fh, vbq);
  309. mutex_lock(&vbq->vb_lock);
  310. for (i = 0; i < count; i++) {
  311. err = omap24xxcam_vbq_alloc_mmap_buffer(vbq->bufs[i]);
  312. if (err)
  313. goto out;
  314. dev_dbg(fh->cam->dev, "sglen is %d for buffer %d\n",
  315. videobuf_to_dma(vbq->bufs[i])->sglen, i);
  316. }
  317. mutex_unlock(&vbq->vb_lock);
  318. return 0;
  319. out:
  320. while (i) {
  321. i--;
  322. omap24xxcam_vbq_free_mmap_buffer(vbq->bufs[i]);
  323. }
  324. mutex_unlock(&vbq->vb_lock);
  325. return err;
  326. }
  327. /*
  328. * This routine is called from interrupt context when a scatter-gather DMA
  329. * transfer of a videobuf_buffer completes.
  330. */
  331. static void omap24xxcam_vbq_complete(struct omap24xxcam_sgdma *sgdma,
  332. u32 csr, void *arg)
  333. {
  334. struct omap24xxcam_device *cam =
  335. container_of(sgdma, struct omap24xxcam_device, sgdma);
  336. struct omap24xxcam_fh *fh = cam->streaming->private_data;
  337. struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
  338. const u32 csr_error = CAMDMA_CSR_MISALIGNED_ERR
  339. | CAMDMA_CSR_SUPERVISOR_ERR | CAMDMA_CSR_SECURE_ERR
  340. | CAMDMA_CSR_TRANS_ERR | CAMDMA_CSR_DROP;
  341. unsigned long flags;
  342. spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
  343. if (--cam->sgdma_in_queue == 0)
  344. omap24xxcam_core_disable(cam);
  345. spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
  346. do_gettimeofday(&vb->ts);
  347. vb->field_count = atomic_add_return(2, &fh->field_count);
  348. if (csr & csr_error) {
  349. vb->state = VIDEOBUF_ERROR;
  350. if (!atomic_read(&fh->cam->in_reset)) {
  351. dev_dbg(cam->dev, "resetting camera, csr 0x%x\n", csr);
  352. omap24xxcam_reset(cam);
  353. }
  354. } else
  355. vb->state = VIDEOBUF_DONE;
  356. wake_up(&vb->done);
  357. }
  358. static void omap24xxcam_vbq_release(struct videobuf_queue *vbq,
  359. struct videobuf_buffer *vb)
  360. {
  361. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  362. /* wait for buffer, especially to get out of the sgdma queue */
  363. videobuf_waiton(vbq, vb, 0, 0);
  364. if (vb->memory == V4L2_MEMORY_MMAP) {
  365. dma_unmap_sg(vbq->dev, dma->sglist, dma->sglen,
  366. dma->direction);
  367. dma->direction = DMA_NONE;
  368. } else {
  369. videobuf_dma_unmap(vbq->dev, videobuf_to_dma(vb));
  370. videobuf_dma_free(videobuf_to_dma(vb));
  371. }
  372. vb->state = VIDEOBUF_NEEDS_INIT;
  373. }
  374. /*
  375. * Limit the number of available kernel image capture buffers based on the
  376. * number requested, the currently selected image size, and the maximum
  377. * amount of memory permitted for kernel capture buffers.
  378. */
  379. static int omap24xxcam_vbq_setup(struct videobuf_queue *vbq, unsigned int *cnt,
  380. unsigned int *size)
  381. {
  382. struct omap24xxcam_fh *fh = vbq->priv_data;
  383. if (*cnt <= 0)
  384. *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
  385. if (*cnt > VIDEO_MAX_FRAME)
  386. *cnt = VIDEO_MAX_FRAME;
  387. *size = fh->pix.sizeimage;
  388. /* accessing fh->cam->capture_mem is ok, it's constant */
  389. if (*size * *cnt > fh->cam->capture_mem)
  390. *cnt = fh->cam->capture_mem / *size;
  391. return 0;
  392. }
  393. static int omap24xxcam_dma_iolock(struct videobuf_queue *vbq,
  394. struct videobuf_dmabuf *dma)
  395. {
  396. int err = 0;
  397. dma->direction = PCI_DMA_FROMDEVICE;
  398. if (!dma_map_sg(vbq->dev, dma->sglist, dma->sglen, dma->direction)) {
  399. kfree(dma->sglist);
  400. dma->sglist = NULL;
  401. dma->sglen = 0;
  402. err = -EIO;
  403. }
  404. return err;
  405. }
  406. static int omap24xxcam_vbq_prepare(struct videobuf_queue *vbq,
  407. struct videobuf_buffer *vb,
  408. enum v4l2_field field)
  409. {
  410. struct omap24xxcam_fh *fh = vbq->priv_data;
  411. int err = 0;
  412. /*
  413. * Accessing pix here is okay since it's constant while
  414. * streaming is on (and we only get called then).
  415. */
  416. if (vb->baddr) {
  417. /* This is a userspace buffer. */
  418. if (fh->pix.sizeimage > vb->bsize) {
  419. /* The buffer isn't big enough. */
  420. err = -EINVAL;
  421. } else
  422. vb->size = fh->pix.sizeimage;
  423. } else {
  424. if (vb->state != VIDEOBUF_NEEDS_INIT) {
  425. /*
  426. * We have a kernel bounce buffer that has
  427. * already been allocated.
  428. */
  429. if (fh->pix.sizeimage > vb->size) {
  430. /*
  431. * The image size has been changed to
  432. * a larger size since this buffer was
  433. * allocated, so we need to free and
  434. * reallocate it.
  435. */
  436. omap24xxcam_vbq_release(vbq, vb);
  437. vb->size = fh->pix.sizeimage;
  438. }
  439. } else {
  440. /* We need to allocate a new kernel bounce buffer. */
  441. vb->size = fh->pix.sizeimage;
  442. }
  443. }
  444. if (err)
  445. return err;
  446. vb->width = fh->pix.width;
  447. vb->height = fh->pix.height;
  448. vb->field = field;
  449. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  450. if (vb->memory == V4L2_MEMORY_MMAP)
  451. /*
  452. * we have built the scatter-gather list by ourself so
  453. * do the scatter-gather mapping as well
  454. */
  455. err = omap24xxcam_dma_iolock(vbq, videobuf_to_dma(vb));
  456. else
  457. err = videobuf_iolock(vbq, vb, NULL);
  458. }
  459. if (!err)
  460. vb->state = VIDEOBUF_PREPARED;
  461. else
  462. omap24xxcam_vbq_release(vbq, vb);
  463. return err;
  464. }
  465. static void omap24xxcam_vbq_queue(struct videobuf_queue *vbq,
  466. struct videobuf_buffer *vb)
  467. {
  468. struct omap24xxcam_fh *fh = vbq->priv_data;
  469. struct omap24xxcam_device *cam = fh->cam;
  470. enum videobuf_state state = vb->state;
  471. unsigned long flags;
  472. int err;
  473. /*
  474. * FIXME: We're marking the buffer active since we have no
  475. * pretty way of marking it active exactly when the
  476. * scatter-gather transfer starts.
  477. */
  478. vb->state = VIDEOBUF_ACTIVE;
  479. err = omap24xxcam_sgdma_queue(&fh->cam->sgdma,
  480. videobuf_to_dma(vb)->sglist,
  481. videobuf_to_dma(vb)->sglen, vb->size,
  482. omap24xxcam_vbq_complete, vb);
  483. if (!err) {
  484. spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
  485. if (++cam->sgdma_in_queue == 1
  486. && !atomic_read(&cam->in_reset))
  487. omap24xxcam_core_enable(cam);
  488. spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
  489. } else {
  490. /*
  491. * Oops. We're not supposed to get any errors here.
  492. * The only way we could get an error is if we ran out
  493. * of scatter-gather DMA slots, but we are supposed to
  494. * have at least as many scatter-gather DMA slots as
  495. * video buffers so that can't happen.
  496. */
  497. dev_err(cam->dev, "failed to queue a video buffer for dma!\n");
  498. dev_err(cam->dev, "likely a bug in the driver!\n");
  499. vb->state = state;
  500. }
  501. }
  502. static struct videobuf_queue_ops omap24xxcam_vbq_ops = {
  503. .buf_setup = omap24xxcam_vbq_setup,
  504. .buf_prepare = omap24xxcam_vbq_prepare,
  505. .buf_queue = omap24xxcam_vbq_queue,
  506. .buf_release = omap24xxcam_vbq_release,
  507. };
  508. /*
  509. *
  510. * OMAP main camera system
  511. *
  512. */
  513. /*
  514. * Reset camera block to power-on state.
  515. */
  516. static void omap24xxcam_poweron_reset(struct omap24xxcam_device *cam)
  517. {
  518. int max_loop = RESET_TIMEOUT_NS;
  519. /* Reset whole camera subsystem */
  520. omap24xxcam_reg_out(cam->mmio_base,
  521. CAM_SYSCONFIG,
  522. CAM_SYSCONFIG_SOFTRESET);
  523. /* Wait till it's finished */
  524. while (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
  525. & CAM_SYSSTATUS_RESETDONE)
  526. && --max_loop) {
  527. ndelay(1);
  528. }
  529. if (!(omap24xxcam_reg_in(cam->mmio_base, CAM_SYSSTATUS)
  530. & CAM_SYSSTATUS_RESETDONE))
  531. dev_err(cam->dev, "camera soft reset timeout\n");
  532. }
  533. /*
  534. * (Re)initialise the camera block.
  535. */
  536. static void omap24xxcam_hwinit(struct omap24xxcam_device *cam)
  537. {
  538. omap24xxcam_poweron_reset(cam);
  539. /* set the camera subsystem autoidle bit */
  540. omap24xxcam_reg_out(cam->mmio_base, CAM_SYSCONFIG,
  541. CAM_SYSCONFIG_AUTOIDLE);
  542. /* set the camera MMU autoidle bit */
  543. omap24xxcam_reg_out(cam->mmio_base,
  544. CAMMMU_REG_OFFSET + CAMMMU_SYSCONFIG,
  545. CAMMMU_SYSCONFIG_AUTOIDLE);
  546. omap24xxcam_core_hwinit(cam);
  547. omap24xxcam_dma_hwinit(&cam->sgdma.dma);
  548. }
  549. /*
  550. * Callback for dma transfer stalling.
  551. */
  552. static void omap24xxcam_stalled_dma_reset(unsigned long data)
  553. {
  554. struct omap24xxcam_device *cam = (struct omap24xxcam_device *)data;
  555. if (!atomic_read(&cam->in_reset)) {
  556. dev_dbg(cam->dev, "dma stalled, resetting camera\n");
  557. omap24xxcam_reset(cam);
  558. }
  559. }
  560. /*
  561. * Stop capture. Mark we're doing a reset, stop DMA transfers and
  562. * core. (No new scatter-gather transfers will be queued whilst
  563. * in_reset is non-zero.)
  564. *
  565. * If omap24xxcam_capture_stop is called from several places at
  566. * once, only the first call will have an effect. Similarly, the last
  567. * call omap24xxcam_streaming_cont will have effect.
  568. *
  569. * Serialisation is ensured by using cam->core_enable_disable_lock.
  570. */
  571. static void omap24xxcam_capture_stop(struct omap24xxcam_device *cam)
  572. {
  573. unsigned long flags;
  574. spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
  575. if (atomic_inc_return(&cam->in_reset) != 1) {
  576. spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
  577. return;
  578. }
  579. omap24xxcam_core_disable(cam);
  580. spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
  581. omap24xxcam_sgdma_sync(&cam->sgdma);
  582. }
  583. /*
  584. * Reset and continue streaming.
  585. *
  586. * Note: Resetting the camera FIFO via the CC_RST bit in the CC_CTRL
  587. * register is supposed to be sufficient to recover from a camera
  588. * interface error, but it doesn't seem to be enough. If we only do
  589. * that then subsequent image captures are out of sync by either one
  590. * or two times DMA_THRESHOLD bytes. Resetting and re-initializing the
  591. * entire camera subsystem prevents the problem with frame
  592. * synchronization.
  593. */
  594. static void omap24xxcam_capture_cont(struct omap24xxcam_device *cam)
  595. {
  596. unsigned long flags;
  597. spin_lock_irqsave(&cam->core_enable_disable_lock, flags);
  598. if (atomic_read(&cam->in_reset) != 1)
  599. goto out;
  600. omap24xxcam_hwinit(cam);
  601. omap24xxcam_sensor_if_enable(cam);
  602. omap24xxcam_sgdma_process(&cam->sgdma);
  603. if (cam->sgdma_in_queue)
  604. omap24xxcam_core_enable(cam);
  605. out:
  606. atomic_dec(&cam->in_reset);
  607. spin_unlock_irqrestore(&cam->core_enable_disable_lock, flags);
  608. }
  609. static ssize_t
  610. omap24xxcam_streaming_show(struct device *dev, struct device_attribute *attr,
  611. char *buf)
  612. {
  613. struct omap24xxcam_device *cam = dev_get_drvdata(dev);
  614. return sprintf(buf, "%s\n", cam->streaming ? "active" : "inactive");
  615. }
  616. static DEVICE_ATTR(streaming, S_IRUGO, omap24xxcam_streaming_show, NULL);
  617. /*
  618. * Stop capture and restart it. I.e. reset the camera during use.
  619. */
  620. static void omap24xxcam_reset(struct omap24xxcam_device *cam)
  621. {
  622. omap24xxcam_capture_stop(cam);
  623. omap24xxcam_capture_cont(cam);
  624. }
  625. /*
  626. * The main interrupt handler.
  627. */
  628. static irqreturn_t omap24xxcam_isr(int irq, void *arg)
  629. {
  630. struct omap24xxcam_device *cam = (struct omap24xxcam_device *)arg;
  631. u32 irqstatus;
  632. unsigned int irqhandled = 0;
  633. irqstatus = omap24xxcam_reg_in(cam->mmio_base, CAM_IRQSTATUS);
  634. if (irqstatus &
  635. (CAM_IRQSTATUS_DMA_IRQ2 | CAM_IRQSTATUS_DMA_IRQ1
  636. | CAM_IRQSTATUS_DMA_IRQ0)) {
  637. omap24xxcam_dma_isr(&cam->sgdma.dma);
  638. irqhandled = 1;
  639. }
  640. if (irqstatus & CAM_IRQSTATUS_CC_IRQ) {
  641. omap24xxcam_core_isr(cam);
  642. irqhandled = 1;
  643. }
  644. if (irqstatus & CAM_IRQSTATUS_MMU_IRQ)
  645. dev_err(cam->dev, "unhandled camera MMU interrupt!\n");
  646. return IRQ_RETVAL(irqhandled);
  647. }
  648. /*
  649. *
  650. * Sensor handling.
  651. *
  652. */
  653. /*
  654. * Enable the external sensor interface. Try to negotiate interface
  655. * parameters with the sensor and start using the new ones. The calls
  656. * to sensor_if_enable and sensor_if_disable need not to be balanced.
  657. */
  658. static int omap24xxcam_sensor_if_enable(struct omap24xxcam_device *cam)
  659. {
  660. int rval;
  661. struct v4l2_ifparm p;
  662. rval = vidioc_int_g_ifparm(cam->sdev, &p);
  663. if (rval) {
  664. dev_err(cam->dev, "vidioc_int_g_ifparm failed with %d\n", rval);
  665. return rval;
  666. }
  667. cam->if_type = p.if_type;
  668. cam->cc_ctrl = CC_CTRL_CC_EN;
  669. switch (p.if_type) {
  670. case V4L2_IF_TYPE_BT656:
  671. if (p.u.bt656.frame_start_on_rising_vs)
  672. cam->cc_ctrl |= CC_CTRL_NOBT_SYNCHRO;
  673. if (p.u.bt656.bt_sync_correct)
  674. cam->cc_ctrl |= CC_CTRL_BT_CORRECT;
  675. if (p.u.bt656.swap)
  676. cam->cc_ctrl |= CC_CTRL_PAR_ORDERCAM;
  677. if (p.u.bt656.latch_clk_inv)
  678. cam->cc_ctrl |= CC_CTRL_PAR_CLK_POL;
  679. if (p.u.bt656.nobt_hs_inv)
  680. cam->cc_ctrl |= CC_CTRL_NOBT_HS_POL;
  681. if (p.u.bt656.nobt_vs_inv)
  682. cam->cc_ctrl |= CC_CTRL_NOBT_VS_POL;
  683. switch (p.u.bt656.mode) {
  684. case V4L2_IF_TYPE_BT656_MODE_NOBT_8BIT:
  685. cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT8;
  686. break;
  687. case V4L2_IF_TYPE_BT656_MODE_NOBT_10BIT:
  688. cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT10;
  689. break;
  690. case V4L2_IF_TYPE_BT656_MODE_NOBT_12BIT:
  691. cam->cc_ctrl |= CC_CTRL_PAR_MODE_NOBT12;
  692. break;
  693. case V4L2_IF_TYPE_BT656_MODE_BT_8BIT:
  694. cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT8;
  695. break;
  696. case V4L2_IF_TYPE_BT656_MODE_BT_10BIT:
  697. cam->cc_ctrl |= CC_CTRL_PAR_MODE_BT10;
  698. break;
  699. default:
  700. dev_err(cam->dev,
  701. "bt656 interface mode %d not supported\n",
  702. p.u.bt656.mode);
  703. return -EINVAL;
  704. }
  705. /*
  706. * The clock rate that the sensor wants has changed.
  707. * We have to adjust the xclk from OMAP 2 side to
  708. * match the sensor's wish as closely as possible.
  709. */
  710. if (p.u.bt656.clock_curr != cam->if_u.bt656.xclk) {
  711. u32 xclk = p.u.bt656.clock_curr;
  712. u32 divisor;
  713. if (xclk == 0)
  714. return -EINVAL;
  715. if (xclk > CAM_MCLK)
  716. xclk = CAM_MCLK;
  717. divisor = CAM_MCLK / xclk;
  718. if (divisor * xclk < CAM_MCLK)
  719. divisor++;
  720. if (CAM_MCLK / divisor < p.u.bt656.clock_min
  721. && divisor > 1)
  722. divisor--;
  723. if (divisor > 30)
  724. divisor = 30;
  725. xclk = CAM_MCLK / divisor;
  726. if (xclk < p.u.bt656.clock_min
  727. || xclk > p.u.bt656.clock_max)
  728. return -EINVAL;
  729. cam->if_u.bt656.xclk = xclk;
  730. }
  731. omap24xxcam_core_xclk_set(cam, cam->if_u.bt656.xclk);
  732. break;
  733. default:
  734. /* FIXME: how about other interfaces? */
  735. dev_err(cam->dev, "interface type %d not supported\n",
  736. p.if_type);
  737. return -EINVAL;
  738. }
  739. return 0;
  740. }
  741. static void omap24xxcam_sensor_if_disable(const struct omap24xxcam_device *cam)
  742. {
  743. switch (cam->if_type) {
  744. case V4L2_IF_TYPE_BT656:
  745. omap24xxcam_core_xclk_set(cam, 0);
  746. break;
  747. }
  748. }
  749. /*
  750. * Initialise the sensor hardware.
  751. */
  752. static int omap24xxcam_sensor_init(struct omap24xxcam_device *cam)
  753. {
  754. int err = 0;
  755. struct v4l2_int_device *sdev = cam->sdev;
  756. omap24xxcam_clock_on(cam);
  757. err = omap24xxcam_sensor_if_enable(cam);
  758. if (err) {
  759. dev_err(cam->dev, "sensor interface could not be enabled at "
  760. "initialisation, %d\n", err);
  761. cam->sdev = NULL;
  762. goto out;
  763. }
  764. /* power up sensor during sensor initialization */
  765. vidioc_int_s_power(sdev, 1);
  766. err = vidioc_int_dev_init(sdev);
  767. if (err) {
  768. dev_err(cam->dev, "cannot initialize sensor, error %d\n", err);
  769. /* Sensor init failed --- it's nonexistent to us! */
  770. cam->sdev = NULL;
  771. goto out;
  772. }
  773. dev_info(cam->dev, "sensor is %s\n", sdev->name);
  774. out:
  775. omap24xxcam_sensor_if_disable(cam);
  776. omap24xxcam_clock_off(cam);
  777. vidioc_int_s_power(sdev, 0);
  778. return err;
  779. }
  780. static void omap24xxcam_sensor_exit(struct omap24xxcam_device *cam)
  781. {
  782. if (cam->sdev)
  783. vidioc_int_dev_exit(cam->sdev);
  784. }
  785. static void omap24xxcam_sensor_disable(struct omap24xxcam_device *cam)
  786. {
  787. omap24xxcam_sensor_if_disable(cam);
  788. omap24xxcam_clock_off(cam);
  789. vidioc_int_s_power(cam->sdev, 0);
  790. }
  791. /*
  792. * Power-up and configure camera sensor. It's ready for capturing now.
  793. */
  794. static int omap24xxcam_sensor_enable(struct omap24xxcam_device *cam)
  795. {
  796. int rval;
  797. omap24xxcam_clock_on(cam);
  798. omap24xxcam_sensor_if_enable(cam);
  799. rval = vidioc_int_s_power(cam->sdev, 1);
  800. if (rval)
  801. goto out;
  802. rval = vidioc_int_init(cam->sdev);
  803. if (rval)
  804. goto out;
  805. return 0;
  806. out:
  807. omap24xxcam_sensor_disable(cam);
  808. return rval;
  809. }
  810. static void omap24xxcam_sensor_reset_work(struct work_struct *work)
  811. {
  812. struct omap24xxcam_device *cam =
  813. container_of(work, struct omap24xxcam_device,
  814. sensor_reset_work);
  815. if (atomic_read(&cam->reset_disable))
  816. return;
  817. omap24xxcam_capture_stop(cam);
  818. if (vidioc_int_reset(cam->sdev) == 0) {
  819. vidioc_int_init(cam->sdev);
  820. } else {
  821. /* Can't reset it by vidioc_int_reset. */
  822. omap24xxcam_sensor_disable(cam);
  823. omap24xxcam_sensor_enable(cam);
  824. }
  825. omap24xxcam_capture_cont(cam);
  826. }
  827. /*
  828. *
  829. * IOCTL interface.
  830. *
  831. */
  832. static int vidioc_querycap(struct file *file, void *fh,
  833. struct v4l2_capability *cap)
  834. {
  835. struct omap24xxcam_fh *ofh = fh;
  836. struct omap24xxcam_device *cam = ofh->cam;
  837. strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
  838. strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
  839. cap->version = OMAP24XXCAM_VERSION;
  840. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  841. return 0;
  842. }
  843. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
  844. struct v4l2_fmtdesc *f)
  845. {
  846. struct omap24xxcam_fh *ofh = fh;
  847. struct omap24xxcam_device *cam = ofh->cam;
  848. int rval;
  849. rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
  850. return rval;
  851. }
  852. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
  853. struct v4l2_format *f)
  854. {
  855. struct omap24xxcam_fh *ofh = fh;
  856. struct omap24xxcam_device *cam = ofh->cam;
  857. int rval;
  858. mutex_lock(&cam->mutex);
  859. rval = vidioc_int_g_fmt_cap(cam->sdev, f);
  860. mutex_unlock(&cam->mutex);
  861. return rval;
  862. }
  863. static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
  864. struct v4l2_format *f)
  865. {
  866. struct omap24xxcam_fh *ofh = fh;
  867. struct omap24xxcam_device *cam = ofh->cam;
  868. int rval;
  869. mutex_lock(&cam->mutex);
  870. if (cam->streaming) {
  871. rval = -EBUSY;
  872. goto out;
  873. }
  874. rval = vidioc_int_s_fmt_cap(cam->sdev, f);
  875. out:
  876. mutex_unlock(&cam->mutex);
  877. if (!rval) {
  878. mutex_lock(&ofh->vbq.vb_lock);
  879. ofh->pix = f->fmt.pix;
  880. mutex_unlock(&ofh->vbq.vb_lock);
  881. }
  882. memset(f, 0, sizeof(*f));
  883. vidioc_g_fmt_vid_cap(file, fh, f);
  884. return rval;
  885. }
  886. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
  887. struct v4l2_format *f)
  888. {
  889. struct omap24xxcam_fh *ofh = fh;
  890. struct omap24xxcam_device *cam = ofh->cam;
  891. int rval;
  892. mutex_lock(&cam->mutex);
  893. rval = vidioc_int_try_fmt_cap(cam->sdev, f);
  894. mutex_unlock(&cam->mutex);
  895. return rval;
  896. }
  897. static int vidioc_reqbufs(struct file *file, void *fh,
  898. struct v4l2_requestbuffers *b)
  899. {
  900. struct omap24xxcam_fh *ofh = fh;
  901. struct omap24xxcam_device *cam = ofh->cam;
  902. int rval;
  903. mutex_lock(&cam->mutex);
  904. if (cam->streaming) {
  905. mutex_unlock(&cam->mutex);
  906. return -EBUSY;
  907. }
  908. omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
  909. mutex_unlock(&cam->mutex);
  910. rval = videobuf_reqbufs(&ofh->vbq, b);
  911. /*
  912. * Either videobuf_reqbufs failed or the buffers are not
  913. * memory-mapped (which would need special attention).
  914. */
  915. if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
  916. goto out;
  917. rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
  918. if (rval)
  919. omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
  920. out:
  921. return rval;
  922. }
  923. static int vidioc_querybuf(struct file *file, void *fh,
  924. struct v4l2_buffer *b)
  925. {
  926. struct omap24xxcam_fh *ofh = fh;
  927. return videobuf_querybuf(&ofh->vbq, b);
  928. }
  929. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  930. {
  931. struct omap24xxcam_fh *ofh = fh;
  932. return videobuf_qbuf(&ofh->vbq, b);
  933. }
  934. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  935. {
  936. struct omap24xxcam_fh *ofh = fh;
  937. struct omap24xxcam_device *cam = ofh->cam;
  938. struct videobuf_buffer *vb;
  939. int rval;
  940. videobuf_dqbuf_again:
  941. rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
  942. if (rval)
  943. goto out;
  944. vb = ofh->vbq.bufs[b->index];
  945. mutex_lock(&cam->mutex);
  946. /* _needs_reset returns -EIO if reset is required. */
  947. rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
  948. mutex_unlock(&cam->mutex);
  949. if (rval == -EIO)
  950. schedule_work(&cam->sensor_reset_work);
  951. else
  952. rval = 0;
  953. out:
  954. /*
  955. * This is a hack. We don't want to show -EIO to the user
  956. * space. Requeue the buffer and try again if we're not doing
  957. * this in non-blocking mode.
  958. */
  959. if (rval == -EIO) {
  960. videobuf_qbuf(&ofh->vbq, b);
  961. if (!(file->f_flags & O_NONBLOCK))
  962. goto videobuf_dqbuf_again;
  963. /*
  964. * We don't have a videobuf_buffer now --- maybe next
  965. * time...
  966. */
  967. rval = -EAGAIN;
  968. }
  969. return rval;
  970. }
  971. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  972. {
  973. struct omap24xxcam_fh *ofh = fh;
  974. struct omap24xxcam_device *cam = ofh->cam;
  975. int rval;
  976. mutex_lock(&cam->mutex);
  977. if (cam->streaming) {
  978. rval = -EBUSY;
  979. goto out;
  980. }
  981. rval = omap24xxcam_sensor_if_enable(cam);
  982. if (rval) {
  983. dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
  984. goto out;
  985. }
  986. rval = videobuf_streamon(&ofh->vbq);
  987. if (!rval) {
  988. cam->streaming = file;
  989. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  990. }
  991. out:
  992. mutex_unlock(&cam->mutex);
  993. return rval;
  994. }
  995. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  996. {
  997. struct omap24xxcam_fh *ofh = fh;
  998. struct omap24xxcam_device *cam = ofh->cam;
  999. struct videobuf_queue *q = &ofh->vbq;
  1000. int rval;
  1001. atomic_inc(&cam->reset_disable);
  1002. flush_work_sync(&cam->sensor_reset_work);
  1003. rval = videobuf_streamoff(q);
  1004. if (!rval) {
  1005. mutex_lock(&cam->mutex);
  1006. cam->streaming = NULL;
  1007. mutex_unlock(&cam->mutex);
  1008. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  1009. }
  1010. atomic_dec(&cam->reset_disable);
  1011. return rval;
  1012. }
  1013. static int vidioc_enum_input(struct file *file, void *fh,
  1014. struct v4l2_input *inp)
  1015. {
  1016. if (inp->index > 0)
  1017. return -EINVAL;
  1018. strlcpy(inp->name, "camera", sizeof(inp->name));
  1019. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1020. return 0;
  1021. }
  1022. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  1023. {
  1024. *i = 0;
  1025. return 0;
  1026. }
  1027. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  1028. {
  1029. if (i > 0)
  1030. return -EINVAL;
  1031. return 0;
  1032. }
  1033. static int vidioc_queryctrl(struct file *file, void *fh,
  1034. struct v4l2_queryctrl *a)
  1035. {
  1036. struct omap24xxcam_fh *ofh = fh;
  1037. struct omap24xxcam_device *cam = ofh->cam;
  1038. int rval;
  1039. rval = vidioc_int_queryctrl(cam->sdev, a);
  1040. return rval;
  1041. }
  1042. static int vidioc_g_ctrl(struct file *file, void *fh,
  1043. struct v4l2_control *a)
  1044. {
  1045. struct omap24xxcam_fh *ofh = fh;
  1046. struct omap24xxcam_device *cam = ofh->cam;
  1047. int rval;
  1048. mutex_lock(&cam->mutex);
  1049. rval = vidioc_int_g_ctrl(cam->sdev, a);
  1050. mutex_unlock(&cam->mutex);
  1051. return rval;
  1052. }
  1053. static int vidioc_s_ctrl(struct file *file, void *fh,
  1054. struct v4l2_control *a)
  1055. {
  1056. struct omap24xxcam_fh *ofh = fh;
  1057. struct omap24xxcam_device *cam = ofh->cam;
  1058. int rval;
  1059. mutex_lock(&cam->mutex);
  1060. rval = vidioc_int_s_ctrl(cam->sdev, a);
  1061. mutex_unlock(&cam->mutex);
  1062. return rval;
  1063. }
  1064. static int vidioc_g_parm(struct file *file, void *fh,
  1065. struct v4l2_streamparm *a) {
  1066. struct omap24xxcam_fh *ofh = fh;
  1067. struct omap24xxcam_device *cam = ofh->cam;
  1068. int rval;
  1069. mutex_lock(&cam->mutex);
  1070. rval = vidioc_int_g_parm(cam->sdev, a);
  1071. mutex_unlock(&cam->mutex);
  1072. return rval;
  1073. }
  1074. static int vidioc_s_parm(struct file *file, void *fh,
  1075. struct v4l2_streamparm *a)
  1076. {
  1077. struct omap24xxcam_fh *ofh = fh;
  1078. struct omap24xxcam_device *cam = ofh->cam;
  1079. struct v4l2_streamparm old_streamparm;
  1080. int rval;
  1081. mutex_lock(&cam->mutex);
  1082. if (cam->streaming) {
  1083. rval = -EBUSY;
  1084. goto out;
  1085. }
  1086. old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1087. rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
  1088. if (rval)
  1089. goto out;
  1090. rval = vidioc_int_s_parm(cam->sdev, a);
  1091. if (rval)
  1092. goto out;
  1093. rval = omap24xxcam_sensor_if_enable(cam);
  1094. /*
  1095. * Revert to old streaming parameters if enabling sensor
  1096. * interface with the new ones failed.
  1097. */
  1098. if (rval)
  1099. vidioc_int_s_parm(cam->sdev, &old_streamparm);
  1100. out:
  1101. mutex_unlock(&cam->mutex);
  1102. return rval;
  1103. }
  1104. /*
  1105. *
  1106. * File operations.
  1107. *
  1108. */
  1109. static unsigned int omap24xxcam_poll(struct file *file,
  1110. struct poll_table_struct *wait)
  1111. {
  1112. struct omap24xxcam_fh *fh = file->private_data;
  1113. struct omap24xxcam_device *cam = fh->cam;
  1114. struct videobuf_buffer *vb;
  1115. mutex_lock(&cam->mutex);
  1116. if (cam->streaming != file) {
  1117. mutex_unlock(&cam->mutex);
  1118. return POLLERR;
  1119. }
  1120. mutex_unlock(&cam->mutex);
  1121. mutex_lock(&fh->vbq.vb_lock);
  1122. if (list_empty(&fh->vbq.stream)) {
  1123. mutex_unlock(&fh->vbq.vb_lock);
  1124. return POLLERR;
  1125. }
  1126. vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
  1127. mutex_unlock(&fh->vbq.vb_lock);
  1128. poll_wait(file, &vb->done, wait);
  1129. if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
  1130. return POLLIN | POLLRDNORM;
  1131. return 0;
  1132. }
  1133. static int omap24xxcam_mmap_buffers(struct file *file,
  1134. struct vm_area_struct *vma)
  1135. {
  1136. struct omap24xxcam_fh *fh = file->private_data;
  1137. struct omap24xxcam_device *cam = fh->cam;
  1138. struct videobuf_queue *vbq = &fh->vbq;
  1139. unsigned int first, last, size, i, j;
  1140. int err = 0;
  1141. mutex_lock(&cam->mutex);
  1142. if (cam->streaming) {
  1143. mutex_unlock(&cam->mutex);
  1144. return -EBUSY;
  1145. }
  1146. mutex_unlock(&cam->mutex);
  1147. mutex_lock(&vbq->vb_lock);
  1148. /* look for first buffer to map */
  1149. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  1150. if (NULL == vbq->bufs[first])
  1151. continue;
  1152. if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
  1153. continue;
  1154. if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  1155. break;
  1156. }
  1157. /* look for last buffer to map */
  1158. for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
  1159. if (NULL == vbq->bufs[last])
  1160. continue;
  1161. if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
  1162. continue;
  1163. size += vbq->bufs[last]->bsize;
  1164. if (size == (vma->vm_end - vma->vm_start))
  1165. break;
  1166. }
  1167. size = 0;
  1168. for (i = first; i <= last && i < VIDEO_MAX_FRAME; i++) {
  1169. struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
  1170. for (j = 0; j < dma->sglen; j++) {
  1171. err = remap_pfn_range(
  1172. vma, vma->vm_start + size,
  1173. page_to_pfn(sg_page(&dma->sglist[j])),
  1174. sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
  1175. if (err)
  1176. goto out;
  1177. size += sg_dma_len(&dma->sglist[j]);
  1178. }
  1179. }
  1180. out:
  1181. mutex_unlock(&vbq->vb_lock);
  1182. return err;
  1183. }
  1184. static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
  1185. {
  1186. struct omap24xxcam_fh *fh = file->private_data;
  1187. int rval;
  1188. /* let the video-buf mapper check arguments and set-up structures */
  1189. rval = videobuf_mmap_mapper(&fh->vbq, vma);
  1190. if (rval)
  1191. return rval;
  1192. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1193. /* do mapping to our allocated buffers */
  1194. rval = omap24xxcam_mmap_buffers(file, vma);
  1195. /*
  1196. * In case of error, free vma->vm_private_data allocated by
  1197. * videobuf_mmap_mapper.
  1198. */
  1199. if (rval)
  1200. kfree(vma->vm_private_data);
  1201. return rval;
  1202. }
  1203. static int omap24xxcam_open(struct file *file)
  1204. {
  1205. struct omap24xxcam_device *cam = omap24xxcam.priv;
  1206. struct omap24xxcam_fh *fh;
  1207. struct v4l2_format format;
  1208. if (!cam || !cam->vfd)
  1209. return -ENODEV;
  1210. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1211. if (fh == NULL)
  1212. return -ENOMEM;
  1213. mutex_lock(&cam->mutex);
  1214. if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
  1215. mutex_unlock(&cam->mutex);
  1216. goto out_try_module_get;
  1217. }
  1218. if (atomic_inc_return(&cam->users) == 1) {
  1219. omap24xxcam_hwinit(cam);
  1220. if (omap24xxcam_sensor_enable(cam)) {
  1221. mutex_unlock(&cam->mutex);
  1222. goto out_omap24xxcam_sensor_enable;
  1223. }
  1224. }
  1225. mutex_unlock(&cam->mutex);
  1226. fh->cam = cam;
  1227. mutex_lock(&cam->mutex);
  1228. vidioc_int_g_fmt_cap(cam->sdev, &format);
  1229. mutex_unlock(&cam->mutex);
  1230. /* FIXME: how about fh->pix when there are more users? */
  1231. fh->pix = format.fmt.pix;
  1232. file->private_data = fh;
  1233. spin_lock_init(&fh->vbq_lock);
  1234. videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
  1235. &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1236. V4L2_FIELD_NONE,
  1237. sizeof(struct videobuf_buffer), fh, NULL);
  1238. return 0;
  1239. out_omap24xxcam_sensor_enable:
  1240. omap24xxcam_poweron_reset(cam);
  1241. module_put(cam->sdev->module);
  1242. out_try_module_get:
  1243. kfree(fh);
  1244. return -ENODEV;
  1245. }
  1246. static int omap24xxcam_release(struct file *file)
  1247. {
  1248. struct omap24xxcam_fh *fh = file->private_data;
  1249. struct omap24xxcam_device *cam = fh->cam;
  1250. atomic_inc(&cam->reset_disable);
  1251. flush_work_sync(&cam->sensor_reset_work);
  1252. /* stop streaming capture */
  1253. videobuf_streamoff(&fh->vbq);
  1254. mutex_lock(&cam->mutex);
  1255. if (cam->streaming == file) {
  1256. cam->streaming = NULL;
  1257. mutex_unlock(&cam->mutex);
  1258. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  1259. } else {
  1260. mutex_unlock(&cam->mutex);
  1261. }
  1262. atomic_dec(&cam->reset_disable);
  1263. omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
  1264. /*
  1265. * Make sure the reset work we might have scheduled is not
  1266. * pending! It may be run *only* if we have users. (And it may
  1267. * not be scheduled anymore since streaming is already
  1268. * disabled.)
  1269. */
  1270. flush_work_sync(&cam->sensor_reset_work);
  1271. mutex_lock(&cam->mutex);
  1272. if (atomic_dec_return(&cam->users) == 0) {
  1273. omap24xxcam_sensor_disable(cam);
  1274. omap24xxcam_poweron_reset(cam);
  1275. }
  1276. mutex_unlock(&cam->mutex);
  1277. file->private_data = NULL;
  1278. module_put(cam->sdev->module);
  1279. kfree(fh);
  1280. return 0;
  1281. }
  1282. static struct v4l2_file_operations omap24xxcam_fops = {
  1283. .ioctl = video_ioctl2,
  1284. .poll = omap24xxcam_poll,
  1285. .mmap = omap24xxcam_mmap,
  1286. .open = omap24xxcam_open,
  1287. .release = omap24xxcam_release,
  1288. };
  1289. /*
  1290. *
  1291. * Power management.
  1292. *
  1293. */
  1294. #ifdef CONFIG_PM
  1295. static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
  1296. {
  1297. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1298. if (atomic_read(&cam->users) == 0)
  1299. return 0;
  1300. if (!atomic_read(&cam->reset_disable))
  1301. omap24xxcam_capture_stop(cam);
  1302. omap24xxcam_sensor_disable(cam);
  1303. omap24xxcam_poweron_reset(cam);
  1304. return 0;
  1305. }
  1306. static int omap24xxcam_resume(struct platform_device *pdev)
  1307. {
  1308. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1309. if (atomic_read(&cam->users) == 0)
  1310. return 0;
  1311. omap24xxcam_hwinit(cam);
  1312. omap24xxcam_sensor_enable(cam);
  1313. if (!atomic_read(&cam->reset_disable))
  1314. omap24xxcam_capture_cont(cam);
  1315. return 0;
  1316. }
  1317. #endif /* CONFIG_PM */
  1318. static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops = {
  1319. .vidioc_querycap = vidioc_querycap,
  1320. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1321. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1322. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1323. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1324. .vidioc_reqbufs = vidioc_reqbufs,
  1325. .vidioc_querybuf = vidioc_querybuf,
  1326. .vidioc_qbuf = vidioc_qbuf,
  1327. .vidioc_dqbuf = vidioc_dqbuf,
  1328. .vidioc_streamon = vidioc_streamon,
  1329. .vidioc_streamoff = vidioc_streamoff,
  1330. .vidioc_enum_input = vidioc_enum_input,
  1331. .vidioc_g_input = vidioc_g_input,
  1332. .vidioc_s_input = vidioc_s_input,
  1333. .vidioc_queryctrl = vidioc_queryctrl,
  1334. .vidioc_g_ctrl = vidioc_g_ctrl,
  1335. .vidioc_s_ctrl = vidioc_s_ctrl,
  1336. .vidioc_g_parm = vidioc_g_parm,
  1337. .vidioc_s_parm = vidioc_s_parm,
  1338. };
  1339. /*
  1340. *
  1341. * Camera device (i.e. /dev/video).
  1342. *
  1343. */
  1344. static int omap24xxcam_device_register(struct v4l2_int_device *s)
  1345. {
  1346. struct omap24xxcam_device *cam = s->u.slave->master->priv;
  1347. struct video_device *vfd;
  1348. int rval;
  1349. /* We already have a slave. */
  1350. if (cam->sdev)
  1351. return -EBUSY;
  1352. cam->sdev = s;
  1353. if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
  1354. dev_err(cam->dev, "could not register sysfs entry\n");
  1355. rval = -EBUSY;
  1356. goto err;
  1357. }
  1358. /* initialize the video_device struct */
  1359. vfd = cam->vfd = video_device_alloc();
  1360. if (!vfd) {
  1361. dev_err(cam->dev, "could not allocate video device struct\n");
  1362. rval = -ENOMEM;
  1363. goto err;
  1364. }
  1365. vfd->release = video_device_release;
  1366. vfd->parent = cam->dev;
  1367. strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
  1368. vfd->fops = &omap24xxcam_fops;
  1369. vfd->ioctl_ops = &omap24xxcam_ioctl_fops;
  1370. omap24xxcam_hwinit(cam);
  1371. rval = omap24xxcam_sensor_init(cam);
  1372. if (rval)
  1373. goto err;
  1374. if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
  1375. dev_err(cam->dev, "could not register V4L device\n");
  1376. rval = -EBUSY;
  1377. goto err;
  1378. }
  1379. omap24xxcam_poweron_reset(cam);
  1380. dev_info(cam->dev, "registered device %s\n",
  1381. video_device_node_name(vfd));
  1382. return 0;
  1383. err:
  1384. omap24xxcam_device_unregister(s);
  1385. return rval;
  1386. }
  1387. static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
  1388. {
  1389. struct omap24xxcam_device *cam = s->u.slave->master->priv;
  1390. omap24xxcam_sensor_exit(cam);
  1391. if (cam->vfd) {
  1392. if (!video_is_registered(cam->vfd)) {
  1393. /*
  1394. * The device was never registered, so release the
  1395. * video_device struct directly.
  1396. */
  1397. video_device_release(cam->vfd);
  1398. } else {
  1399. /*
  1400. * The unregister function will release the
  1401. * video_device struct as well as
  1402. * unregistering it.
  1403. */
  1404. video_unregister_device(cam->vfd);
  1405. }
  1406. cam->vfd = NULL;
  1407. }
  1408. device_remove_file(cam->dev, &dev_attr_streaming);
  1409. cam->sdev = NULL;
  1410. }
  1411. static struct v4l2_int_master omap24xxcam_master = {
  1412. .attach = omap24xxcam_device_register,
  1413. .detach = omap24xxcam_device_unregister,
  1414. };
  1415. static struct v4l2_int_device omap24xxcam = {
  1416. .module = THIS_MODULE,
  1417. .name = CAM_NAME,
  1418. .type = v4l2_int_type_master,
  1419. .u = {
  1420. .master = &omap24xxcam_master
  1421. },
  1422. };
  1423. /*
  1424. *
  1425. * Driver initialisation and deinitialisation.
  1426. *
  1427. */
  1428. static int __devinit omap24xxcam_probe(struct platform_device *pdev)
  1429. {
  1430. struct omap24xxcam_device *cam;
  1431. struct resource *mem;
  1432. int irq;
  1433. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  1434. if (!cam) {
  1435. dev_err(&pdev->dev, "could not allocate memory\n");
  1436. goto err;
  1437. }
  1438. platform_set_drvdata(pdev, cam);
  1439. cam->dev = &pdev->dev;
  1440. /*
  1441. * Impose a lower limit on the amount of memory allocated for
  1442. * capture. We require at least enough memory to double-buffer
  1443. * QVGA (300KB).
  1444. */
  1445. if (capture_mem < 320 * 240 * 2 * 2)
  1446. capture_mem = 320 * 240 * 2 * 2;
  1447. cam->capture_mem = capture_mem;
  1448. /* request the mem region for the camera registers */
  1449. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1450. if (!mem) {
  1451. dev_err(cam->dev, "no mem resource?\n");
  1452. goto err;
  1453. }
  1454. if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
  1455. pdev->name)) {
  1456. dev_err(cam->dev,
  1457. "cannot reserve camera register I/O region\n");
  1458. goto err;
  1459. }
  1460. cam->mmio_base_phys = mem->start;
  1461. cam->mmio_size = (mem->end - mem->start) + 1;
  1462. /* map the region */
  1463. cam->mmio_base = (unsigned long)
  1464. ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
  1465. if (!cam->mmio_base) {
  1466. dev_err(cam->dev, "cannot map camera register I/O region\n");
  1467. goto err;
  1468. }
  1469. irq = platform_get_irq(pdev, 0);
  1470. if (irq <= 0) {
  1471. dev_err(cam->dev, "no irq for camera?\n");
  1472. goto err;
  1473. }
  1474. /* install the interrupt service routine */
  1475. if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
  1476. dev_err(cam->dev,
  1477. "could not install interrupt service routine\n");
  1478. goto err;
  1479. }
  1480. cam->irq = irq;
  1481. if (omap24xxcam_clock_get(cam))
  1482. goto err;
  1483. INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
  1484. mutex_init(&cam->mutex);
  1485. spin_lock_init(&cam->core_enable_disable_lock);
  1486. omap24xxcam_sgdma_init(&cam->sgdma,
  1487. cam->mmio_base + CAMDMA_REG_OFFSET,
  1488. omap24xxcam_stalled_dma_reset,
  1489. (unsigned long)cam);
  1490. omap24xxcam.priv = cam;
  1491. if (v4l2_int_device_register(&omap24xxcam))
  1492. goto err;
  1493. return 0;
  1494. err:
  1495. omap24xxcam_remove(pdev);
  1496. return -ENODEV;
  1497. }
  1498. static int omap24xxcam_remove(struct platform_device *pdev)
  1499. {
  1500. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1501. if (!cam)
  1502. return 0;
  1503. if (omap24xxcam.priv != NULL)
  1504. v4l2_int_device_unregister(&omap24xxcam);
  1505. omap24xxcam.priv = NULL;
  1506. omap24xxcam_clock_put(cam);
  1507. if (cam->irq) {
  1508. free_irq(cam->irq, cam);
  1509. cam->irq = 0;
  1510. }
  1511. if (cam->mmio_base) {
  1512. iounmap((void *)cam->mmio_base);
  1513. cam->mmio_base = 0;
  1514. }
  1515. if (cam->mmio_base_phys) {
  1516. release_mem_region(cam->mmio_base_phys, cam->mmio_size);
  1517. cam->mmio_base_phys = 0;
  1518. }
  1519. kfree(cam);
  1520. return 0;
  1521. }
  1522. static struct platform_driver omap24xxcam_driver = {
  1523. .probe = omap24xxcam_probe,
  1524. .remove = omap24xxcam_remove,
  1525. #ifdef CONFIG_PM
  1526. .suspend = omap24xxcam_suspend,
  1527. .resume = omap24xxcam_resume,
  1528. #endif
  1529. .driver = {
  1530. .name = CAM_NAME,
  1531. .owner = THIS_MODULE,
  1532. },
  1533. };
  1534. /*
  1535. *
  1536. * Module initialisation and deinitialisation
  1537. *
  1538. */
  1539. static int __init omap24xxcam_init(void)
  1540. {
  1541. return platform_driver_register(&omap24xxcam_driver);
  1542. }
  1543. static void __exit omap24xxcam_cleanup(void)
  1544. {
  1545. platform_driver_unregister(&omap24xxcam_driver);
  1546. }
  1547. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
  1548. MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
  1549. MODULE_LICENSE("GPL");
  1550. module_param(video_nr, int, 0);
  1551. MODULE_PARM_DESC(video_nr,
  1552. "Minor number for video device (-1 ==> auto assign)");
  1553. module_param(capture_mem, int, 0);
  1554. MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
  1555. "buffers (default 4800kiB)");
  1556. module_init(omap24xxcam_init);
  1557. module_exit(omap24xxcam_cleanup);