omap24xxcam.c 44 KB

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