omap24xxcam.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  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 <linux/module.h>
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ioctl.h>
  41. #include "omap24xxcam.h"
  42. #define OMAP24XXCAM_VERSION "0.0.1"
  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, order);
  270. /* if allocation fails, try to allocate smaller amount */
  271. while (page == NULL) {
  272. order--;
  273. page = alloc_pages(GFP_KERNEL, 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->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  840. return 0;
  841. }
  842. static int vidioc_enum_fmt_vid_cap(struct file *file, void *fh,
  843. struct v4l2_fmtdesc *f)
  844. {
  845. struct omap24xxcam_fh *ofh = fh;
  846. struct omap24xxcam_device *cam = ofh->cam;
  847. int rval;
  848. rval = vidioc_int_enum_fmt_cap(cam->sdev, f);
  849. return rval;
  850. }
  851. static int vidioc_g_fmt_vid_cap(struct file *file, void *fh,
  852. struct v4l2_format *f)
  853. {
  854. struct omap24xxcam_fh *ofh = fh;
  855. struct omap24xxcam_device *cam = ofh->cam;
  856. int rval;
  857. mutex_lock(&cam->mutex);
  858. rval = vidioc_int_g_fmt_cap(cam->sdev, f);
  859. mutex_unlock(&cam->mutex);
  860. return rval;
  861. }
  862. static int vidioc_s_fmt_vid_cap(struct file *file, void *fh,
  863. struct v4l2_format *f)
  864. {
  865. struct omap24xxcam_fh *ofh = fh;
  866. struct omap24xxcam_device *cam = ofh->cam;
  867. int rval;
  868. mutex_lock(&cam->mutex);
  869. if (cam->streaming) {
  870. rval = -EBUSY;
  871. goto out;
  872. }
  873. rval = vidioc_int_s_fmt_cap(cam->sdev, f);
  874. out:
  875. mutex_unlock(&cam->mutex);
  876. if (!rval) {
  877. mutex_lock(&ofh->vbq.vb_lock);
  878. ofh->pix = f->fmt.pix;
  879. mutex_unlock(&ofh->vbq.vb_lock);
  880. }
  881. memset(f, 0, sizeof(*f));
  882. vidioc_g_fmt_vid_cap(file, fh, f);
  883. return rval;
  884. }
  885. static int vidioc_try_fmt_vid_cap(struct file *file, void *fh,
  886. struct v4l2_format *f)
  887. {
  888. struct omap24xxcam_fh *ofh = fh;
  889. struct omap24xxcam_device *cam = ofh->cam;
  890. int rval;
  891. mutex_lock(&cam->mutex);
  892. rval = vidioc_int_try_fmt_cap(cam->sdev, f);
  893. mutex_unlock(&cam->mutex);
  894. return rval;
  895. }
  896. static int vidioc_reqbufs(struct file *file, void *fh,
  897. struct v4l2_requestbuffers *b)
  898. {
  899. struct omap24xxcam_fh *ofh = fh;
  900. struct omap24xxcam_device *cam = ofh->cam;
  901. int rval;
  902. mutex_lock(&cam->mutex);
  903. if (cam->streaming) {
  904. mutex_unlock(&cam->mutex);
  905. return -EBUSY;
  906. }
  907. omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
  908. mutex_unlock(&cam->mutex);
  909. rval = videobuf_reqbufs(&ofh->vbq, b);
  910. /*
  911. * Either videobuf_reqbufs failed or the buffers are not
  912. * memory-mapped (which would need special attention).
  913. */
  914. if (rval < 0 || b->memory != V4L2_MEMORY_MMAP)
  915. goto out;
  916. rval = omap24xxcam_vbq_alloc_mmap_buffers(&ofh->vbq, rval);
  917. if (rval)
  918. omap24xxcam_vbq_free_mmap_buffers(&ofh->vbq);
  919. out:
  920. return rval;
  921. }
  922. static int vidioc_querybuf(struct file *file, void *fh,
  923. struct v4l2_buffer *b)
  924. {
  925. struct omap24xxcam_fh *ofh = fh;
  926. return videobuf_querybuf(&ofh->vbq, b);
  927. }
  928. static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  929. {
  930. struct omap24xxcam_fh *ofh = fh;
  931. return videobuf_qbuf(&ofh->vbq, b);
  932. }
  933. static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
  934. {
  935. struct omap24xxcam_fh *ofh = fh;
  936. struct omap24xxcam_device *cam = ofh->cam;
  937. struct videobuf_buffer *vb;
  938. int rval;
  939. videobuf_dqbuf_again:
  940. rval = videobuf_dqbuf(&ofh->vbq, b, file->f_flags & O_NONBLOCK);
  941. if (rval)
  942. goto out;
  943. vb = ofh->vbq.bufs[b->index];
  944. mutex_lock(&cam->mutex);
  945. /* _needs_reset returns -EIO if reset is required. */
  946. rval = vidioc_int_g_needs_reset(cam->sdev, (void *)vb->baddr);
  947. mutex_unlock(&cam->mutex);
  948. if (rval == -EIO)
  949. schedule_work(&cam->sensor_reset_work);
  950. else
  951. rval = 0;
  952. out:
  953. /*
  954. * This is a hack. We don't want to show -EIO to the user
  955. * space. Requeue the buffer and try again if we're not doing
  956. * this in non-blocking mode.
  957. */
  958. if (rval == -EIO) {
  959. videobuf_qbuf(&ofh->vbq, b);
  960. if (!(file->f_flags & O_NONBLOCK))
  961. goto videobuf_dqbuf_again;
  962. /*
  963. * We don't have a videobuf_buffer now --- maybe next
  964. * time...
  965. */
  966. rval = -EAGAIN;
  967. }
  968. return rval;
  969. }
  970. static int vidioc_streamon(struct file *file, void *fh, enum v4l2_buf_type i)
  971. {
  972. struct omap24xxcam_fh *ofh = fh;
  973. struct omap24xxcam_device *cam = ofh->cam;
  974. int rval;
  975. mutex_lock(&cam->mutex);
  976. if (cam->streaming) {
  977. rval = -EBUSY;
  978. goto out;
  979. }
  980. rval = omap24xxcam_sensor_if_enable(cam);
  981. if (rval) {
  982. dev_dbg(cam->dev, "vidioc_int_g_ifparm failed\n");
  983. goto out;
  984. }
  985. rval = videobuf_streamon(&ofh->vbq);
  986. if (!rval) {
  987. cam->streaming = file;
  988. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  989. }
  990. out:
  991. mutex_unlock(&cam->mutex);
  992. return rval;
  993. }
  994. static int vidioc_streamoff(struct file *file, void *fh, enum v4l2_buf_type i)
  995. {
  996. struct omap24xxcam_fh *ofh = fh;
  997. struct omap24xxcam_device *cam = ofh->cam;
  998. struct videobuf_queue *q = &ofh->vbq;
  999. int rval;
  1000. atomic_inc(&cam->reset_disable);
  1001. flush_work_sync(&cam->sensor_reset_work);
  1002. rval = videobuf_streamoff(q);
  1003. if (!rval) {
  1004. mutex_lock(&cam->mutex);
  1005. cam->streaming = NULL;
  1006. mutex_unlock(&cam->mutex);
  1007. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  1008. }
  1009. atomic_dec(&cam->reset_disable);
  1010. return rval;
  1011. }
  1012. static int vidioc_enum_input(struct file *file, void *fh,
  1013. struct v4l2_input *inp)
  1014. {
  1015. if (inp->index > 0)
  1016. return -EINVAL;
  1017. strlcpy(inp->name, "camera", sizeof(inp->name));
  1018. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1019. return 0;
  1020. }
  1021. static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
  1022. {
  1023. *i = 0;
  1024. return 0;
  1025. }
  1026. static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
  1027. {
  1028. if (i > 0)
  1029. return -EINVAL;
  1030. return 0;
  1031. }
  1032. static int vidioc_queryctrl(struct file *file, void *fh,
  1033. struct v4l2_queryctrl *a)
  1034. {
  1035. struct omap24xxcam_fh *ofh = fh;
  1036. struct omap24xxcam_device *cam = ofh->cam;
  1037. int rval;
  1038. rval = vidioc_int_queryctrl(cam->sdev, a);
  1039. return rval;
  1040. }
  1041. static int vidioc_g_ctrl(struct file *file, void *fh,
  1042. struct v4l2_control *a)
  1043. {
  1044. struct omap24xxcam_fh *ofh = fh;
  1045. struct omap24xxcam_device *cam = ofh->cam;
  1046. int rval;
  1047. mutex_lock(&cam->mutex);
  1048. rval = vidioc_int_g_ctrl(cam->sdev, a);
  1049. mutex_unlock(&cam->mutex);
  1050. return rval;
  1051. }
  1052. static int vidioc_s_ctrl(struct file *file, void *fh,
  1053. struct v4l2_control *a)
  1054. {
  1055. struct omap24xxcam_fh *ofh = fh;
  1056. struct omap24xxcam_device *cam = ofh->cam;
  1057. int rval;
  1058. mutex_lock(&cam->mutex);
  1059. rval = vidioc_int_s_ctrl(cam->sdev, a);
  1060. mutex_unlock(&cam->mutex);
  1061. return rval;
  1062. }
  1063. static int vidioc_g_parm(struct file *file, void *fh,
  1064. struct v4l2_streamparm *a) {
  1065. struct omap24xxcam_fh *ofh = fh;
  1066. struct omap24xxcam_device *cam = ofh->cam;
  1067. int rval;
  1068. mutex_lock(&cam->mutex);
  1069. rval = vidioc_int_g_parm(cam->sdev, a);
  1070. mutex_unlock(&cam->mutex);
  1071. return rval;
  1072. }
  1073. static int vidioc_s_parm(struct file *file, void *fh,
  1074. struct v4l2_streamparm *a)
  1075. {
  1076. struct omap24xxcam_fh *ofh = fh;
  1077. struct omap24xxcam_device *cam = ofh->cam;
  1078. struct v4l2_streamparm old_streamparm;
  1079. int rval;
  1080. mutex_lock(&cam->mutex);
  1081. if (cam->streaming) {
  1082. rval = -EBUSY;
  1083. goto out;
  1084. }
  1085. old_streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1086. rval = vidioc_int_g_parm(cam->sdev, &old_streamparm);
  1087. if (rval)
  1088. goto out;
  1089. rval = vidioc_int_s_parm(cam->sdev, a);
  1090. if (rval)
  1091. goto out;
  1092. rval = omap24xxcam_sensor_if_enable(cam);
  1093. /*
  1094. * Revert to old streaming parameters if enabling sensor
  1095. * interface with the new ones failed.
  1096. */
  1097. if (rval)
  1098. vidioc_int_s_parm(cam->sdev, &old_streamparm);
  1099. out:
  1100. mutex_unlock(&cam->mutex);
  1101. return rval;
  1102. }
  1103. /*
  1104. *
  1105. * File operations.
  1106. *
  1107. */
  1108. static unsigned int omap24xxcam_poll(struct file *file,
  1109. struct poll_table_struct *wait)
  1110. {
  1111. struct omap24xxcam_fh *fh = file->private_data;
  1112. struct omap24xxcam_device *cam = fh->cam;
  1113. struct videobuf_buffer *vb;
  1114. mutex_lock(&cam->mutex);
  1115. if (cam->streaming != file) {
  1116. mutex_unlock(&cam->mutex);
  1117. return POLLERR;
  1118. }
  1119. mutex_unlock(&cam->mutex);
  1120. mutex_lock(&fh->vbq.vb_lock);
  1121. if (list_empty(&fh->vbq.stream)) {
  1122. mutex_unlock(&fh->vbq.vb_lock);
  1123. return POLLERR;
  1124. }
  1125. vb = list_entry(fh->vbq.stream.next, struct videobuf_buffer, stream);
  1126. mutex_unlock(&fh->vbq.vb_lock);
  1127. poll_wait(file, &vb->done, wait);
  1128. if (vb->state == VIDEOBUF_DONE || vb->state == VIDEOBUF_ERROR)
  1129. return POLLIN | POLLRDNORM;
  1130. return 0;
  1131. }
  1132. static int omap24xxcam_mmap_buffers(struct file *file,
  1133. struct vm_area_struct *vma)
  1134. {
  1135. struct omap24xxcam_fh *fh = file->private_data;
  1136. struct omap24xxcam_device *cam = fh->cam;
  1137. struct videobuf_queue *vbq = &fh->vbq;
  1138. unsigned int first, last, size, i, j;
  1139. int err = 0;
  1140. mutex_lock(&cam->mutex);
  1141. if (cam->streaming) {
  1142. mutex_unlock(&cam->mutex);
  1143. return -EBUSY;
  1144. }
  1145. mutex_unlock(&cam->mutex);
  1146. mutex_lock(&vbq->vb_lock);
  1147. /* look for first buffer to map */
  1148. for (first = 0; first < VIDEO_MAX_FRAME; first++) {
  1149. if (NULL == vbq->bufs[first])
  1150. continue;
  1151. if (V4L2_MEMORY_MMAP != vbq->bufs[first]->memory)
  1152. continue;
  1153. if (vbq->bufs[first]->boff == (vma->vm_pgoff << PAGE_SHIFT))
  1154. break;
  1155. }
  1156. /* look for last buffer to map */
  1157. for (size = 0, last = first; last < VIDEO_MAX_FRAME; last++) {
  1158. if (NULL == vbq->bufs[last])
  1159. continue;
  1160. if (V4L2_MEMORY_MMAP != vbq->bufs[last]->memory)
  1161. continue;
  1162. size += vbq->bufs[last]->bsize;
  1163. if (size == (vma->vm_end - vma->vm_start))
  1164. break;
  1165. }
  1166. size = 0;
  1167. for (i = first; i <= last && i < VIDEO_MAX_FRAME; i++) {
  1168. struct videobuf_dmabuf *dma = videobuf_to_dma(vbq->bufs[i]);
  1169. for (j = 0; j < dma->sglen; j++) {
  1170. err = remap_pfn_range(
  1171. vma, vma->vm_start + size,
  1172. page_to_pfn(sg_page(&dma->sglist[j])),
  1173. sg_dma_len(&dma->sglist[j]), vma->vm_page_prot);
  1174. if (err)
  1175. goto out;
  1176. size += sg_dma_len(&dma->sglist[j]);
  1177. }
  1178. }
  1179. out:
  1180. mutex_unlock(&vbq->vb_lock);
  1181. return err;
  1182. }
  1183. static int omap24xxcam_mmap(struct file *file, struct vm_area_struct *vma)
  1184. {
  1185. struct omap24xxcam_fh *fh = file->private_data;
  1186. int rval;
  1187. /* let the video-buf mapper check arguments and set-up structures */
  1188. rval = videobuf_mmap_mapper(&fh->vbq, vma);
  1189. if (rval)
  1190. return rval;
  1191. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1192. /* do mapping to our allocated buffers */
  1193. rval = omap24xxcam_mmap_buffers(file, vma);
  1194. /*
  1195. * In case of error, free vma->vm_private_data allocated by
  1196. * videobuf_mmap_mapper.
  1197. */
  1198. if (rval)
  1199. kfree(vma->vm_private_data);
  1200. return rval;
  1201. }
  1202. static int omap24xxcam_open(struct file *file)
  1203. {
  1204. struct omap24xxcam_device *cam = omap24xxcam.priv;
  1205. struct omap24xxcam_fh *fh;
  1206. struct v4l2_format format;
  1207. if (!cam || !cam->vfd)
  1208. return -ENODEV;
  1209. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  1210. if (fh == NULL)
  1211. return -ENOMEM;
  1212. mutex_lock(&cam->mutex);
  1213. if (cam->sdev == NULL || !try_module_get(cam->sdev->module)) {
  1214. mutex_unlock(&cam->mutex);
  1215. goto out_try_module_get;
  1216. }
  1217. if (atomic_inc_return(&cam->users) == 1) {
  1218. omap24xxcam_hwinit(cam);
  1219. if (omap24xxcam_sensor_enable(cam)) {
  1220. mutex_unlock(&cam->mutex);
  1221. goto out_omap24xxcam_sensor_enable;
  1222. }
  1223. }
  1224. mutex_unlock(&cam->mutex);
  1225. fh->cam = cam;
  1226. mutex_lock(&cam->mutex);
  1227. vidioc_int_g_fmt_cap(cam->sdev, &format);
  1228. mutex_unlock(&cam->mutex);
  1229. /* FIXME: how about fh->pix when there are more users? */
  1230. fh->pix = format.fmt.pix;
  1231. file->private_data = fh;
  1232. spin_lock_init(&fh->vbq_lock);
  1233. videobuf_queue_sg_init(&fh->vbq, &omap24xxcam_vbq_ops, NULL,
  1234. &fh->vbq_lock, V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1235. V4L2_FIELD_NONE,
  1236. sizeof(struct videobuf_buffer), fh, NULL);
  1237. return 0;
  1238. out_omap24xxcam_sensor_enable:
  1239. omap24xxcam_poweron_reset(cam);
  1240. module_put(cam->sdev->module);
  1241. out_try_module_get:
  1242. kfree(fh);
  1243. return -ENODEV;
  1244. }
  1245. static int omap24xxcam_release(struct file *file)
  1246. {
  1247. struct omap24xxcam_fh *fh = file->private_data;
  1248. struct omap24xxcam_device *cam = fh->cam;
  1249. atomic_inc(&cam->reset_disable);
  1250. flush_work_sync(&cam->sensor_reset_work);
  1251. /* stop streaming capture */
  1252. videobuf_streamoff(&fh->vbq);
  1253. mutex_lock(&cam->mutex);
  1254. if (cam->streaming == file) {
  1255. cam->streaming = NULL;
  1256. mutex_unlock(&cam->mutex);
  1257. sysfs_notify(&cam->dev->kobj, NULL, "streaming");
  1258. } else {
  1259. mutex_unlock(&cam->mutex);
  1260. }
  1261. atomic_dec(&cam->reset_disable);
  1262. omap24xxcam_vbq_free_mmap_buffers(&fh->vbq);
  1263. /*
  1264. * Make sure the reset work we might have scheduled is not
  1265. * pending! It may be run *only* if we have users. (And it may
  1266. * not be scheduled anymore since streaming is already
  1267. * disabled.)
  1268. */
  1269. flush_work_sync(&cam->sensor_reset_work);
  1270. mutex_lock(&cam->mutex);
  1271. if (atomic_dec_return(&cam->users) == 0) {
  1272. omap24xxcam_sensor_disable(cam);
  1273. omap24xxcam_poweron_reset(cam);
  1274. }
  1275. mutex_unlock(&cam->mutex);
  1276. file->private_data = NULL;
  1277. module_put(cam->sdev->module);
  1278. kfree(fh);
  1279. return 0;
  1280. }
  1281. static struct v4l2_file_operations omap24xxcam_fops = {
  1282. .ioctl = video_ioctl2,
  1283. .poll = omap24xxcam_poll,
  1284. .mmap = omap24xxcam_mmap,
  1285. .open = omap24xxcam_open,
  1286. .release = omap24xxcam_release,
  1287. };
  1288. /*
  1289. *
  1290. * Power management.
  1291. *
  1292. */
  1293. #ifdef CONFIG_PM
  1294. static int omap24xxcam_suspend(struct platform_device *pdev, pm_message_t state)
  1295. {
  1296. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1297. if (atomic_read(&cam->users) == 0)
  1298. return 0;
  1299. if (!atomic_read(&cam->reset_disable))
  1300. omap24xxcam_capture_stop(cam);
  1301. omap24xxcam_sensor_disable(cam);
  1302. omap24xxcam_poweron_reset(cam);
  1303. return 0;
  1304. }
  1305. static int omap24xxcam_resume(struct platform_device *pdev)
  1306. {
  1307. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1308. if (atomic_read(&cam->users) == 0)
  1309. return 0;
  1310. omap24xxcam_hwinit(cam);
  1311. omap24xxcam_sensor_enable(cam);
  1312. if (!atomic_read(&cam->reset_disable))
  1313. omap24xxcam_capture_cont(cam);
  1314. return 0;
  1315. }
  1316. #endif /* CONFIG_PM */
  1317. static const struct v4l2_ioctl_ops omap24xxcam_ioctl_fops = {
  1318. .vidioc_querycap = vidioc_querycap,
  1319. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1320. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1321. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1322. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1323. .vidioc_reqbufs = vidioc_reqbufs,
  1324. .vidioc_querybuf = vidioc_querybuf,
  1325. .vidioc_qbuf = vidioc_qbuf,
  1326. .vidioc_dqbuf = vidioc_dqbuf,
  1327. .vidioc_streamon = vidioc_streamon,
  1328. .vidioc_streamoff = vidioc_streamoff,
  1329. .vidioc_enum_input = vidioc_enum_input,
  1330. .vidioc_g_input = vidioc_g_input,
  1331. .vidioc_s_input = vidioc_s_input,
  1332. .vidioc_queryctrl = vidioc_queryctrl,
  1333. .vidioc_g_ctrl = vidioc_g_ctrl,
  1334. .vidioc_s_ctrl = vidioc_s_ctrl,
  1335. .vidioc_g_parm = vidioc_g_parm,
  1336. .vidioc_s_parm = vidioc_s_parm,
  1337. };
  1338. /*
  1339. *
  1340. * Camera device (i.e. /dev/video).
  1341. *
  1342. */
  1343. static int omap24xxcam_device_register(struct v4l2_int_device *s)
  1344. {
  1345. struct omap24xxcam_device *cam = s->u.slave->master->priv;
  1346. struct video_device *vfd;
  1347. int rval;
  1348. /* We already have a slave. */
  1349. if (cam->sdev)
  1350. return -EBUSY;
  1351. cam->sdev = s;
  1352. if (device_create_file(cam->dev, &dev_attr_streaming) != 0) {
  1353. dev_err(cam->dev, "could not register sysfs entry\n");
  1354. rval = -EBUSY;
  1355. goto err;
  1356. }
  1357. /* initialize the video_device struct */
  1358. vfd = cam->vfd = video_device_alloc();
  1359. if (!vfd) {
  1360. dev_err(cam->dev, "could not allocate video device struct\n");
  1361. rval = -ENOMEM;
  1362. goto err;
  1363. }
  1364. vfd->release = video_device_release;
  1365. vfd->parent = cam->dev;
  1366. strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
  1367. vfd->fops = &omap24xxcam_fops;
  1368. vfd->ioctl_ops = &omap24xxcam_ioctl_fops;
  1369. omap24xxcam_hwinit(cam);
  1370. rval = omap24xxcam_sensor_init(cam);
  1371. if (rval)
  1372. goto err;
  1373. if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
  1374. dev_err(cam->dev, "could not register V4L device\n");
  1375. rval = -EBUSY;
  1376. goto err;
  1377. }
  1378. omap24xxcam_poweron_reset(cam);
  1379. dev_info(cam->dev, "registered device %s\n",
  1380. video_device_node_name(vfd));
  1381. return 0;
  1382. err:
  1383. omap24xxcam_device_unregister(s);
  1384. return rval;
  1385. }
  1386. static void omap24xxcam_device_unregister(struct v4l2_int_device *s)
  1387. {
  1388. struct omap24xxcam_device *cam = s->u.slave->master->priv;
  1389. omap24xxcam_sensor_exit(cam);
  1390. if (cam->vfd) {
  1391. if (!video_is_registered(cam->vfd)) {
  1392. /*
  1393. * The device was never registered, so release the
  1394. * video_device struct directly.
  1395. */
  1396. video_device_release(cam->vfd);
  1397. } else {
  1398. /*
  1399. * The unregister function will release the
  1400. * video_device struct as well as
  1401. * unregistering it.
  1402. */
  1403. video_unregister_device(cam->vfd);
  1404. }
  1405. cam->vfd = NULL;
  1406. }
  1407. device_remove_file(cam->dev, &dev_attr_streaming);
  1408. cam->sdev = NULL;
  1409. }
  1410. static struct v4l2_int_master omap24xxcam_master = {
  1411. .attach = omap24xxcam_device_register,
  1412. .detach = omap24xxcam_device_unregister,
  1413. };
  1414. static struct v4l2_int_device omap24xxcam = {
  1415. .module = THIS_MODULE,
  1416. .name = CAM_NAME,
  1417. .type = v4l2_int_type_master,
  1418. .u = {
  1419. .master = &omap24xxcam_master
  1420. },
  1421. };
  1422. /*
  1423. *
  1424. * Driver initialisation and deinitialisation.
  1425. *
  1426. */
  1427. static int __devinit omap24xxcam_probe(struct platform_device *pdev)
  1428. {
  1429. struct omap24xxcam_device *cam;
  1430. struct resource *mem;
  1431. int irq;
  1432. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  1433. if (!cam) {
  1434. dev_err(&pdev->dev, "could not allocate memory\n");
  1435. goto err;
  1436. }
  1437. platform_set_drvdata(pdev, cam);
  1438. cam->dev = &pdev->dev;
  1439. /*
  1440. * Impose a lower limit on the amount of memory allocated for
  1441. * capture. We require at least enough memory to double-buffer
  1442. * QVGA (300KB).
  1443. */
  1444. if (capture_mem < 320 * 240 * 2 * 2)
  1445. capture_mem = 320 * 240 * 2 * 2;
  1446. cam->capture_mem = capture_mem;
  1447. /* request the mem region for the camera registers */
  1448. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1449. if (!mem) {
  1450. dev_err(cam->dev, "no mem resource?\n");
  1451. goto err;
  1452. }
  1453. if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
  1454. dev_err(cam->dev,
  1455. "cannot reserve camera register I/O region\n");
  1456. goto err;
  1457. }
  1458. cam->mmio_base_phys = mem->start;
  1459. cam->mmio_size = resource_size(mem);
  1460. /* map the region */
  1461. cam->mmio_base = (unsigned long)
  1462. ioremap_nocache(cam->mmio_base_phys, cam->mmio_size);
  1463. if (!cam->mmio_base) {
  1464. dev_err(cam->dev, "cannot map camera register I/O region\n");
  1465. goto err;
  1466. }
  1467. irq = platform_get_irq(pdev, 0);
  1468. if (irq <= 0) {
  1469. dev_err(cam->dev, "no irq for camera?\n");
  1470. goto err;
  1471. }
  1472. /* install the interrupt service routine */
  1473. if (request_irq(irq, omap24xxcam_isr, 0, CAM_NAME, cam)) {
  1474. dev_err(cam->dev,
  1475. "could not install interrupt service routine\n");
  1476. goto err;
  1477. }
  1478. cam->irq = irq;
  1479. if (omap24xxcam_clock_get(cam))
  1480. goto err;
  1481. INIT_WORK(&cam->sensor_reset_work, omap24xxcam_sensor_reset_work);
  1482. mutex_init(&cam->mutex);
  1483. spin_lock_init(&cam->core_enable_disable_lock);
  1484. omap24xxcam_sgdma_init(&cam->sgdma,
  1485. cam->mmio_base + CAMDMA_REG_OFFSET,
  1486. omap24xxcam_stalled_dma_reset,
  1487. (unsigned long)cam);
  1488. omap24xxcam.priv = cam;
  1489. if (v4l2_int_device_register(&omap24xxcam))
  1490. goto err;
  1491. return 0;
  1492. err:
  1493. omap24xxcam_remove(pdev);
  1494. return -ENODEV;
  1495. }
  1496. static int omap24xxcam_remove(struct platform_device *pdev)
  1497. {
  1498. struct omap24xxcam_device *cam = platform_get_drvdata(pdev);
  1499. if (!cam)
  1500. return 0;
  1501. if (omap24xxcam.priv != NULL)
  1502. v4l2_int_device_unregister(&omap24xxcam);
  1503. omap24xxcam.priv = NULL;
  1504. omap24xxcam_clock_put(cam);
  1505. if (cam->irq) {
  1506. free_irq(cam->irq, cam);
  1507. cam->irq = 0;
  1508. }
  1509. if (cam->mmio_base) {
  1510. iounmap((void *)cam->mmio_base);
  1511. cam->mmio_base = 0;
  1512. }
  1513. if (cam->mmio_base_phys) {
  1514. release_mem_region(cam->mmio_base_phys, cam->mmio_size);
  1515. cam->mmio_base_phys = 0;
  1516. }
  1517. kfree(cam);
  1518. return 0;
  1519. }
  1520. static struct platform_driver omap24xxcam_driver = {
  1521. .probe = omap24xxcam_probe,
  1522. .remove = omap24xxcam_remove,
  1523. #ifdef CONFIG_PM
  1524. .suspend = omap24xxcam_suspend,
  1525. .resume = omap24xxcam_resume,
  1526. #endif
  1527. .driver = {
  1528. .name = CAM_NAME,
  1529. .owner = THIS_MODULE,
  1530. },
  1531. };
  1532. module_platform_driver(omap24xxcam_driver);
  1533. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@nokia.com>");
  1534. MODULE_DESCRIPTION("OMAP24xx Video for Linux camera driver");
  1535. MODULE_LICENSE("GPL");
  1536. MODULE_VERSION(OMAP24XXCAM_VERSION);
  1537. module_param(video_nr, int, 0);
  1538. MODULE_PARM_DESC(video_nr,
  1539. "Minor number for video device (-1 ==> auto assign)");
  1540. module_param(capture_mem, int, 0);
  1541. MODULE_PARM_DESC(capture_mem, "Maximum amount of memory for capture "
  1542. "buffers (default 4800kiB)");