omap1_camera.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*
  2. * V4L2 SoC Camera driver for OMAP1 Camera Interface
  3. *
  4. * Copyright (C) 2010, Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  5. *
  6. * Based on V4L2 Driver for i.MXL/i.MXL camera (CSI) host
  7. * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  8. * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
  9. *
  10. * Based on PXA SoC camera driver
  11. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  12. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  13. *
  14. * Hardware specific bits initialy based on former work by Matt Callow
  15. * drivers/media/video/omap/omap1510cam.c
  16. * Copyright (C) 2006 Matt Callow
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License version 2 as
  20. * published by the Free Software Foundation.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <media/omap1_camera.h>
  29. #include <media/soc_camera.h>
  30. #include <media/soc_mediabus.h>
  31. #include <media/videobuf-dma-contig.h>
  32. #include <media/videobuf-dma-sg.h>
  33. #include <plat/dma.h>
  34. #define DRIVER_NAME "omap1-camera"
  35. #define DRIVER_VERSION "0.0.2"
  36. /*
  37. * ---------------------------------------------------------------------------
  38. * OMAP1 Camera Interface registers
  39. * ---------------------------------------------------------------------------
  40. */
  41. #define REG_CTRLCLOCK 0x00
  42. #define REG_IT_STATUS 0x04
  43. #define REG_MODE 0x08
  44. #define REG_STATUS 0x0C
  45. #define REG_CAMDATA 0x10
  46. #define REG_GPIO 0x14
  47. #define REG_PEAK_COUNTER 0x18
  48. /* CTRLCLOCK bit shifts */
  49. #define LCLK_EN BIT(7)
  50. #define DPLL_EN BIT(6)
  51. #define MCLK_EN BIT(5)
  52. #define CAMEXCLK_EN BIT(4)
  53. #define POLCLK BIT(3)
  54. #define FOSCMOD_SHIFT 0
  55. #define FOSCMOD_MASK (0x7 << FOSCMOD_SHIFT)
  56. #define FOSCMOD_12MHz 0x0
  57. #define FOSCMOD_6MHz 0x2
  58. #define FOSCMOD_9_6MHz 0x4
  59. #define FOSCMOD_24MHz 0x5
  60. #define FOSCMOD_8MHz 0x6
  61. /* IT_STATUS bit shifts */
  62. #define DATA_TRANSFER BIT(5)
  63. #define FIFO_FULL BIT(4)
  64. #define H_DOWN BIT(3)
  65. #define H_UP BIT(2)
  66. #define V_DOWN BIT(1)
  67. #define V_UP BIT(0)
  68. /* MODE bit shifts */
  69. #define RAZ_FIFO BIT(18)
  70. #define EN_FIFO_FULL BIT(17)
  71. #define EN_NIRQ BIT(16)
  72. #define THRESHOLD_SHIFT 9
  73. #define THRESHOLD_MASK (0x7f << THRESHOLD_SHIFT)
  74. #define DMA BIT(8)
  75. #define EN_H_DOWN BIT(7)
  76. #define EN_H_UP BIT(6)
  77. #define EN_V_DOWN BIT(5)
  78. #define EN_V_UP BIT(4)
  79. #define ORDERCAMD BIT(3)
  80. #define IRQ_MASK (EN_V_UP | EN_V_DOWN | EN_H_UP | EN_H_DOWN | \
  81. EN_NIRQ | EN_FIFO_FULL)
  82. /* STATUS bit shifts */
  83. #define HSTATUS BIT(1)
  84. #define VSTATUS BIT(0)
  85. /* GPIO bit shifts */
  86. #define CAM_RST BIT(0)
  87. /* end of OMAP1 Camera Interface registers */
  88. #define SOCAM_BUS_FLAGS (V4L2_MBUS_MASTER | \
  89. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  90. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING | \
  91. V4L2_MBUS_DATA_ACTIVE_HIGH)
  92. #define FIFO_SIZE ((THRESHOLD_MASK >> THRESHOLD_SHIFT) + 1)
  93. #define FIFO_SHIFT __fls(FIFO_SIZE)
  94. #define DMA_BURST_SHIFT (1 + OMAP_DMA_DATA_BURST_4)
  95. #define DMA_BURST_SIZE (1 << DMA_BURST_SHIFT)
  96. #define DMA_ELEMENT_SHIFT OMAP_DMA_DATA_TYPE_S32
  97. #define DMA_ELEMENT_SIZE (1 << DMA_ELEMENT_SHIFT)
  98. #define DMA_FRAME_SHIFT_CONTIG (FIFO_SHIFT - 1)
  99. #define DMA_FRAME_SHIFT_SG DMA_BURST_SHIFT
  100. #define DMA_FRAME_SHIFT(x) ((x) == OMAP1_CAM_DMA_CONTIG ? \
  101. DMA_FRAME_SHIFT_CONTIG : \
  102. DMA_FRAME_SHIFT_SG)
  103. #define DMA_FRAME_SIZE(x) (1 << DMA_FRAME_SHIFT(x))
  104. #define DMA_SYNC OMAP_DMA_SYNC_FRAME
  105. #define THRESHOLD_LEVEL DMA_FRAME_SIZE
  106. #define MAX_VIDEO_MEM 4 /* arbitrary video memory limit in MB */
  107. /*
  108. * Structures
  109. */
  110. /* buffer for one video frame */
  111. struct omap1_cam_buf {
  112. struct videobuf_buffer vb;
  113. enum v4l2_mbus_pixelcode code;
  114. int inwork;
  115. struct scatterlist *sgbuf;
  116. int sgcount;
  117. int bytes_left;
  118. enum videobuf_state result;
  119. };
  120. struct omap1_cam_dev {
  121. struct soc_camera_host soc_host;
  122. struct soc_camera_device *icd;
  123. struct clk *clk;
  124. unsigned int irq;
  125. void __iomem *base;
  126. int dma_ch;
  127. struct omap1_cam_platform_data *pdata;
  128. struct resource *res;
  129. unsigned long pflags;
  130. unsigned long camexclk;
  131. struct list_head capture;
  132. /* lock used to protect videobuf */
  133. spinlock_t lock;
  134. /* Pointers to DMA buffers */
  135. struct omap1_cam_buf *active;
  136. struct omap1_cam_buf *ready;
  137. enum omap1_cam_vb_mode vb_mode;
  138. int (*mmap_mapper)(struct videobuf_queue *q,
  139. struct videobuf_buffer *buf,
  140. struct vm_area_struct *vma);
  141. u32 reg_cache[0];
  142. };
  143. static void cam_write(struct omap1_cam_dev *pcdev, u16 reg, u32 val)
  144. {
  145. pcdev->reg_cache[reg / sizeof(u32)] = val;
  146. __raw_writel(val, pcdev->base + reg);
  147. }
  148. static u32 cam_read(struct omap1_cam_dev *pcdev, u16 reg, bool from_cache)
  149. {
  150. return !from_cache ? __raw_readl(pcdev->base + reg) :
  151. pcdev->reg_cache[reg / sizeof(u32)];
  152. }
  153. #define CAM_READ(pcdev, reg) \
  154. cam_read(pcdev, REG_##reg, false)
  155. #define CAM_WRITE(pcdev, reg, val) \
  156. cam_write(pcdev, REG_##reg, val)
  157. #define CAM_READ_CACHE(pcdev, reg) \
  158. cam_read(pcdev, REG_##reg, true)
  159. /*
  160. * Videobuf operations
  161. */
  162. static int omap1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  163. unsigned int *size)
  164. {
  165. struct soc_camera_device *icd = vq->priv_data;
  166. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  167. struct omap1_cam_dev *pcdev = ici->priv;
  168. *size = icd->sizeimage;
  169. if (!*count || *count < OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode))
  170. *count = OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode);
  171. if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
  172. *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
  173. dev_dbg(icd->parent,
  174. "%s: count=%d, size=%d\n", __func__, *count, *size);
  175. return 0;
  176. }
  177. static void free_buffer(struct videobuf_queue *vq, struct omap1_cam_buf *buf,
  178. enum omap1_cam_vb_mode vb_mode)
  179. {
  180. struct videobuf_buffer *vb = &buf->vb;
  181. BUG_ON(in_interrupt());
  182. videobuf_waiton(vq, vb, 0, 0);
  183. if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
  184. videobuf_dma_contig_free(vq, vb);
  185. } else {
  186. struct soc_camera_device *icd = vq->priv_data;
  187. struct device *dev = icd->parent;
  188. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  189. videobuf_dma_unmap(dev, dma);
  190. videobuf_dma_free(dma);
  191. }
  192. vb->state = VIDEOBUF_NEEDS_INIT;
  193. }
  194. static int omap1_videobuf_prepare(struct videobuf_queue *vq,
  195. struct videobuf_buffer *vb, enum v4l2_field field)
  196. {
  197. struct soc_camera_device *icd = vq->priv_data;
  198. struct omap1_cam_buf *buf = container_of(vb, struct omap1_cam_buf, vb);
  199. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  200. struct omap1_cam_dev *pcdev = ici->priv;
  201. int ret;
  202. WARN_ON(!list_empty(&vb->queue));
  203. BUG_ON(NULL == icd->current_fmt);
  204. buf->inwork = 1;
  205. if (buf->code != icd->current_fmt->code || vb->field != field ||
  206. vb->width != icd->user_width ||
  207. vb->height != icd->user_height) {
  208. buf->code = icd->current_fmt->code;
  209. vb->width = icd->user_width;
  210. vb->height = icd->user_height;
  211. vb->field = field;
  212. vb->state = VIDEOBUF_NEEDS_INIT;
  213. }
  214. vb->size = icd->sizeimage;
  215. if (vb->baddr && vb->bsize < vb->size) {
  216. ret = -EINVAL;
  217. goto out;
  218. }
  219. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  220. ret = videobuf_iolock(vq, vb, NULL);
  221. if (ret)
  222. goto fail;
  223. vb->state = VIDEOBUF_PREPARED;
  224. }
  225. buf->inwork = 0;
  226. return 0;
  227. fail:
  228. free_buffer(vq, buf, pcdev->vb_mode);
  229. out:
  230. buf->inwork = 0;
  231. return ret;
  232. }
  233. static void set_dma_dest_params(int dma_ch, struct omap1_cam_buf *buf,
  234. enum omap1_cam_vb_mode vb_mode)
  235. {
  236. dma_addr_t dma_addr;
  237. unsigned int block_size;
  238. if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
  239. dma_addr = videobuf_to_dma_contig(&buf->vb);
  240. block_size = buf->vb.size;
  241. } else {
  242. if (WARN_ON(!buf->sgbuf)) {
  243. buf->result = VIDEOBUF_ERROR;
  244. return;
  245. }
  246. dma_addr = sg_dma_address(buf->sgbuf);
  247. if (WARN_ON(!dma_addr)) {
  248. buf->sgbuf = NULL;
  249. buf->result = VIDEOBUF_ERROR;
  250. return;
  251. }
  252. block_size = sg_dma_len(buf->sgbuf);
  253. if (WARN_ON(!block_size)) {
  254. buf->sgbuf = NULL;
  255. buf->result = VIDEOBUF_ERROR;
  256. return;
  257. }
  258. if (unlikely(buf->bytes_left < block_size))
  259. block_size = buf->bytes_left;
  260. if (WARN_ON(dma_addr & (DMA_FRAME_SIZE(vb_mode) *
  261. DMA_ELEMENT_SIZE - 1))) {
  262. dma_addr = ALIGN(dma_addr, DMA_FRAME_SIZE(vb_mode) *
  263. DMA_ELEMENT_SIZE);
  264. block_size &= ~(DMA_FRAME_SIZE(vb_mode) *
  265. DMA_ELEMENT_SIZE - 1);
  266. }
  267. buf->bytes_left -= block_size;
  268. buf->sgcount++;
  269. }
  270. omap_set_dma_dest_params(dma_ch,
  271. OMAP_DMA_PORT_EMIFF, OMAP_DMA_AMODE_POST_INC, dma_addr, 0, 0);
  272. omap_set_dma_transfer_params(dma_ch,
  273. OMAP_DMA_DATA_TYPE_S32, DMA_FRAME_SIZE(vb_mode),
  274. block_size >> (DMA_FRAME_SHIFT(vb_mode) + DMA_ELEMENT_SHIFT),
  275. DMA_SYNC, 0, 0);
  276. }
  277. static struct omap1_cam_buf *prepare_next_vb(struct omap1_cam_dev *pcdev)
  278. {
  279. struct omap1_cam_buf *buf;
  280. /*
  281. * If there is already a buffer pointed out by the pcdev->ready,
  282. * (re)use it, otherwise try to fetch and configure a new one.
  283. */
  284. buf = pcdev->ready;
  285. if (!buf) {
  286. if (list_empty(&pcdev->capture))
  287. return buf;
  288. buf = list_entry(pcdev->capture.next,
  289. struct omap1_cam_buf, vb.queue);
  290. buf->vb.state = VIDEOBUF_ACTIVE;
  291. pcdev->ready = buf;
  292. list_del_init(&buf->vb.queue);
  293. }
  294. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  295. /*
  296. * In CONTIG mode, we can safely enter next buffer parameters
  297. * into the DMA programming register set after the DMA
  298. * has already been activated on the previous buffer
  299. */
  300. set_dma_dest_params(pcdev->dma_ch, buf, pcdev->vb_mode);
  301. } else {
  302. /*
  303. * In SG mode, the above is not safe since there are probably
  304. * a bunch of sgbufs from previous sglist still pending.
  305. * Instead, mark the sglist fresh for the upcoming
  306. * try_next_sgbuf().
  307. */
  308. buf->sgbuf = NULL;
  309. }
  310. return buf;
  311. }
  312. static struct scatterlist *try_next_sgbuf(int dma_ch, struct omap1_cam_buf *buf)
  313. {
  314. struct scatterlist *sgbuf;
  315. if (likely(buf->sgbuf)) {
  316. /* current sglist is active */
  317. if (unlikely(!buf->bytes_left)) {
  318. /* indicate sglist complete */
  319. sgbuf = NULL;
  320. } else {
  321. /* process next sgbuf */
  322. sgbuf = sg_next(buf->sgbuf);
  323. if (WARN_ON(!sgbuf)) {
  324. buf->result = VIDEOBUF_ERROR;
  325. } else if (WARN_ON(!sg_dma_len(sgbuf))) {
  326. sgbuf = NULL;
  327. buf->result = VIDEOBUF_ERROR;
  328. }
  329. }
  330. buf->sgbuf = sgbuf;
  331. } else {
  332. /* sglist is fresh, initialize it before using */
  333. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  334. sgbuf = dma->sglist;
  335. if (!(WARN_ON(!sgbuf))) {
  336. buf->sgbuf = sgbuf;
  337. buf->sgcount = 0;
  338. buf->bytes_left = buf->vb.size;
  339. buf->result = VIDEOBUF_DONE;
  340. }
  341. }
  342. if (sgbuf)
  343. /*
  344. * Put our next sgbuf parameters (address, size)
  345. * into the DMA programming register set.
  346. */
  347. set_dma_dest_params(dma_ch, buf, OMAP1_CAM_DMA_SG);
  348. return sgbuf;
  349. }
  350. static void start_capture(struct omap1_cam_dev *pcdev)
  351. {
  352. struct omap1_cam_buf *buf = pcdev->active;
  353. u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  354. u32 mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN;
  355. if (WARN_ON(!buf))
  356. return;
  357. /*
  358. * Enable start of frame interrupt, which we will use for activating
  359. * our end of frame watchdog when capture actually starts.
  360. */
  361. mode |= EN_V_UP;
  362. if (unlikely(ctrlclock & LCLK_EN))
  363. /* stop pixel clock before FIFO reset */
  364. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  365. /* reset FIFO */
  366. CAM_WRITE(pcdev, MODE, mode | RAZ_FIFO);
  367. omap_start_dma(pcdev->dma_ch);
  368. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  369. /*
  370. * In SG mode, it's a good moment for fetching next sgbuf
  371. * from the current sglist and, if available, already putting
  372. * its parameters into the DMA programming register set.
  373. */
  374. try_next_sgbuf(pcdev->dma_ch, buf);
  375. }
  376. /* (re)enable pixel clock */
  377. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | LCLK_EN);
  378. /* release FIFO reset */
  379. CAM_WRITE(pcdev, MODE, mode);
  380. }
  381. static void suspend_capture(struct omap1_cam_dev *pcdev)
  382. {
  383. u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  384. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  385. omap_stop_dma(pcdev->dma_ch);
  386. }
  387. static void disable_capture(struct omap1_cam_dev *pcdev)
  388. {
  389. u32 mode = CAM_READ_CACHE(pcdev, MODE);
  390. CAM_WRITE(pcdev, MODE, mode & ~(IRQ_MASK | DMA));
  391. }
  392. static void omap1_videobuf_queue(struct videobuf_queue *vq,
  393. struct videobuf_buffer *vb)
  394. {
  395. struct soc_camera_device *icd = vq->priv_data;
  396. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  397. struct omap1_cam_dev *pcdev = ici->priv;
  398. struct omap1_cam_buf *buf;
  399. u32 mode;
  400. list_add_tail(&vb->queue, &pcdev->capture);
  401. vb->state = VIDEOBUF_QUEUED;
  402. if (pcdev->active) {
  403. /*
  404. * Capture in progress, so don't touch pcdev->ready even if
  405. * empty. Since the transfer of the DMA programming register set
  406. * content to the DMA working register set is done automatically
  407. * by the DMA hardware, this can pretty well happen while we
  408. * are keeping the lock here. Leave fetching it from the queue
  409. * to be done when a next DMA interrupt occures instead.
  410. */
  411. return;
  412. }
  413. WARN_ON(pcdev->ready);
  414. buf = prepare_next_vb(pcdev);
  415. if (WARN_ON(!buf))
  416. return;
  417. pcdev->active = buf;
  418. pcdev->ready = NULL;
  419. dev_dbg(icd->parent,
  420. "%s: capture not active, setup FIFO, start DMA\n", __func__);
  421. mode = CAM_READ_CACHE(pcdev, MODE) & ~THRESHOLD_MASK;
  422. mode |= THRESHOLD_LEVEL(pcdev->vb_mode) << THRESHOLD_SHIFT;
  423. CAM_WRITE(pcdev, MODE, mode | EN_FIFO_FULL | DMA);
  424. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  425. /*
  426. * In SG mode, the above prepare_next_vb() didn't actually
  427. * put anything into the DMA programming register set,
  428. * so we have to do it now, before activating DMA.
  429. */
  430. try_next_sgbuf(pcdev->dma_ch, buf);
  431. }
  432. start_capture(pcdev);
  433. }
  434. static void omap1_videobuf_release(struct videobuf_queue *vq,
  435. struct videobuf_buffer *vb)
  436. {
  437. struct omap1_cam_buf *buf =
  438. container_of(vb, struct omap1_cam_buf, vb);
  439. struct soc_camera_device *icd = vq->priv_data;
  440. struct device *dev = icd->parent;
  441. struct soc_camera_host *ici = to_soc_camera_host(dev);
  442. struct omap1_cam_dev *pcdev = ici->priv;
  443. switch (vb->state) {
  444. case VIDEOBUF_DONE:
  445. dev_dbg(dev, "%s (done)\n", __func__);
  446. break;
  447. case VIDEOBUF_ACTIVE:
  448. dev_dbg(dev, "%s (active)\n", __func__);
  449. break;
  450. case VIDEOBUF_QUEUED:
  451. dev_dbg(dev, "%s (queued)\n", __func__);
  452. break;
  453. case VIDEOBUF_PREPARED:
  454. dev_dbg(dev, "%s (prepared)\n", __func__);
  455. break;
  456. default:
  457. dev_dbg(dev, "%s (unknown %d)\n", __func__, vb->state);
  458. break;
  459. }
  460. free_buffer(vq, buf, pcdev->vb_mode);
  461. }
  462. static void videobuf_done(struct omap1_cam_dev *pcdev,
  463. enum videobuf_state result)
  464. {
  465. struct omap1_cam_buf *buf = pcdev->active;
  466. struct videobuf_buffer *vb;
  467. struct device *dev = pcdev->icd->parent;
  468. if (WARN_ON(!buf)) {
  469. suspend_capture(pcdev);
  470. disable_capture(pcdev);
  471. return;
  472. }
  473. if (result == VIDEOBUF_ERROR)
  474. suspend_capture(pcdev);
  475. vb = &buf->vb;
  476. if (waitqueue_active(&vb->done)) {
  477. if (!pcdev->ready && result != VIDEOBUF_ERROR) {
  478. /*
  479. * No next buffer has been entered into the DMA
  480. * programming register set on time (could be done only
  481. * while the previous DMA interurpt was processed, not
  482. * later), so the last DMA block, be it a whole buffer
  483. * if in CONTIG or its last sgbuf if in SG mode, is
  484. * about to be reused by the just autoreinitialized DMA
  485. * engine, and overwritten with next frame data. Best we
  486. * can do is stopping the capture as soon as possible,
  487. * hopefully before the next frame start.
  488. */
  489. suspend_capture(pcdev);
  490. }
  491. vb->state = result;
  492. do_gettimeofday(&vb->ts);
  493. if (result != VIDEOBUF_ERROR)
  494. vb->field_count++;
  495. wake_up(&vb->done);
  496. /* shift in next buffer */
  497. buf = pcdev->ready;
  498. pcdev->active = buf;
  499. pcdev->ready = NULL;
  500. if (!buf) {
  501. /*
  502. * No next buffer was ready on time (see above), so
  503. * indicate error condition to force capture restart or
  504. * stop, depending on next buffer already queued or not.
  505. */
  506. result = VIDEOBUF_ERROR;
  507. prepare_next_vb(pcdev);
  508. buf = pcdev->ready;
  509. pcdev->active = buf;
  510. pcdev->ready = NULL;
  511. }
  512. } else if (pcdev->ready) {
  513. /*
  514. * In both CONTIG and SG mode, the DMA engine has possibly
  515. * been already autoreinitialized with the preprogrammed
  516. * pcdev->ready buffer. We can either accept this fact
  517. * and just swap the buffers, or provoke an error condition
  518. * and restart capture. The former seems less intrusive.
  519. */
  520. dev_dbg(dev, "%s: nobody waiting on videobuf, swap with next\n",
  521. __func__);
  522. pcdev->active = pcdev->ready;
  523. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  524. /*
  525. * In SG mode, we have to make sure that the buffer we
  526. * are putting back into the pcdev->ready is marked
  527. * fresh.
  528. */
  529. buf->sgbuf = NULL;
  530. }
  531. pcdev->ready = buf;
  532. buf = pcdev->active;
  533. } else {
  534. /*
  535. * No next buffer has been entered into
  536. * the DMA programming register set on time.
  537. */
  538. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  539. /*
  540. * In CONTIG mode, the DMA engine has already been
  541. * reinitialized with the current buffer. Best we can do
  542. * is not touching it.
  543. */
  544. dev_dbg(dev,
  545. "%s: nobody waiting on videobuf, reuse it\n",
  546. __func__);
  547. } else {
  548. /*
  549. * In SG mode, the DMA engine has just been
  550. * autoreinitialized with the last sgbuf from the
  551. * current list. Restart capture in order to transfer
  552. * next frame start into the first sgbuf, not the last
  553. * one.
  554. */
  555. if (result != VIDEOBUF_ERROR) {
  556. suspend_capture(pcdev);
  557. result = VIDEOBUF_ERROR;
  558. }
  559. }
  560. }
  561. if (!buf) {
  562. dev_dbg(dev, "%s: no more videobufs, stop capture\n", __func__);
  563. disable_capture(pcdev);
  564. return;
  565. }
  566. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  567. /*
  568. * In CONTIG mode, the current buffer parameters had already
  569. * been entered into the DMA programming register set while the
  570. * buffer was fetched with prepare_next_vb(), they may have also
  571. * been transferred into the runtime set and already active if
  572. * the DMA still running.
  573. */
  574. } else {
  575. /* In SG mode, extra steps are required */
  576. if (result == VIDEOBUF_ERROR)
  577. /* make sure we (re)use sglist from start on error */
  578. buf->sgbuf = NULL;
  579. /*
  580. * In any case, enter the next sgbuf parameters into the DMA
  581. * programming register set. They will be used either during
  582. * nearest DMA autoreinitialization or, in case of an error,
  583. * on DMA startup below.
  584. */
  585. try_next_sgbuf(pcdev->dma_ch, buf);
  586. }
  587. if (result == VIDEOBUF_ERROR) {
  588. dev_dbg(dev, "%s: videobuf error; reset FIFO, restart DMA\n",
  589. __func__);
  590. start_capture(pcdev);
  591. /*
  592. * In SG mode, the above also resulted in the next sgbuf
  593. * parameters being entered into the DMA programming register
  594. * set, making them ready for next DMA autoreinitialization.
  595. */
  596. }
  597. /*
  598. * Finally, try fetching next buffer.
  599. * In CONTIG mode, it will also enter it into the DMA programming
  600. * register set, making it ready for next DMA autoreinitialization.
  601. */
  602. prepare_next_vb(pcdev);
  603. }
  604. static void dma_isr(int channel, unsigned short status, void *data)
  605. {
  606. struct omap1_cam_dev *pcdev = data;
  607. struct omap1_cam_buf *buf = pcdev->active;
  608. unsigned long flags;
  609. spin_lock_irqsave(&pcdev->lock, flags);
  610. if (WARN_ON(!buf)) {
  611. suspend_capture(pcdev);
  612. disable_capture(pcdev);
  613. goto out;
  614. }
  615. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  616. /*
  617. * In CONTIG mode, assume we have just managed to collect the
  618. * whole frame, hopefully before our end of frame watchdog is
  619. * triggered. Then, all we have to do is disabling the watchdog
  620. * for this frame, and calling videobuf_done() with success
  621. * indicated.
  622. */
  623. CAM_WRITE(pcdev, MODE,
  624. CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN);
  625. videobuf_done(pcdev, VIDEOBUF_DONE);
  626. } else {
  627. /*
  628. * In SG mode, we have to process every sgbuf from the current
  629. * sglist, one after another.
  630. */
  631. if (buf->sgbuf) {
  632. /*
  633. * Current sglist not completed yet, try fetching next
  634. * sgbuf, hopefully putting it into the DMA programming
  635. * register set, making it ready for next DMA
  636. * autoreinitialization.
  637. */
  638. try_next_sgbuf(pcdev->dma_ch, buf);
  639. if (buf->sgbuf)
  640. goto out;
  641. /*
  642. * No more sgbufs left in the current sglist. This
  643. * doesn't mean that the whole videobuffer is already
  644. * complete, but only that the last sgbuf from the
  645. * current sglist is about to be filled. It will be
  646. * ready on next DMA interrupt, signalled with the
  647. * buf->sgbuf set back to NULL.
  648. */
  649. if (buf->result != VIDEOBUF_ERROR) {
  650. /*
  651. * Video frame collected without errors so far,
  652. * we can prepare for collecting a next one
  653. * as soon as DMA gets autoreinitialized
  654. * after the current (last) sgbuf is completed.
  655. */
  656. buf = prepare_next_vb(pcdev);
  657. if (!buf)
  658. goto out;
  659. try_next_sgbuf(pcdev->dma_ch, buf);
  660. goto out;
  661. }
  662. }
  663. /* end of videobuf */
  664. videobuf_done(pcdev, buf->result);
  665. }
  666. out:
  667. spin_unlock_irqrestore(&pcdev->lock, flags);
  668. }
  669. static irqreturn_t cam_isr(int irq, void *data)
  670. {
  671. struct omap1_cam_dev *pcdev = data;
  672. struct device *dev = pcdev->icd->parent;
  673. struct omap1_cam_buf *buf = pcdev->active;
  674. u32 it_status;
  675. unsigned long flags;
  676. it_status = CAM_READ(pcdev, IT_STATUS);
  677. if (!it_status)
  678. return IRQ_NONE;
  679. spin_lock_irqsave(&pcdev->lock, flags);
  680. if (WARN_ON(!buf)) {
  681. dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
  682. __func__, it_status);
  683. suspend_capture(pcdev);
  684. disable_capture(pcdev);
  685. goto out;
  686. }
  687. if (unlikely(it_status & FIFO_FULL)) {
  688. dev_warn(dev, "%s: FIFO overflow\n", __func__);
  689. } else if (it_status & V_DOWN) {
  690. /* end of video frame watchdog */
  691. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  692. /*
  693. * In CONTIG mode, the watchdog is disabled with
  694. * successful DMA end of block interrupt, and reenabled
  695. * on next frame start. If we get here, there is nothing
  696. * to check, we must be out of sync.
  697. */
  698. } else {
  699. if (buf->sgcount == 2) {
  700. /*
  701. * If exactly 2 sgbufs from the next sglist have
  702. * been programmed into the DMA engine (the
  703. * first one already transferred into the DMA
  704. * runtime register set, the second one still
  705. * in the programming set), then we are in sync.
  706. */
  707. goto out;
  708. }
  709. }
  710. dev_notice(dev, "%s: unexpected end of video frame\n",
  711. __func__);
  712. } else if (it_status & V_UP) {
  713. u32 mode;
  714. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  715. /*
  716. * In CONTIG mode, we need this interrupt every frame
  717. * in oredr to reenable our end of frame watchdog.
  718. */
  719. mode = CAM_READ_CACHE(pcdev, MODE);
  720. } else {
  721. /*
  722. * In SG mode, the below enabled end of frame watchdog
  723. * is kept on permanently, so we can turn this one shot
  724. * setup off.
  725. */
  726. mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_UP;
  727. }
  728. if (!(mode & EN_V_DOWN)) {
  729. /* (re)enable end of frame watchdog interrupt */
  730. mode |= EN_V_DOWN;
  731. }
  732. CAM_WRITE(pcdev, MODE, mode);
  733. goto out;
  734. } else {
  735. dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
  736. __func__, it_status);
  737. goto out;
  738. }
  739. videobuf_done(pcdev, VIDEOBUF_ERROR);
  740. out:
  741. spin_unlock_irqrestore(&pcdev->lock, flags);
  742. return IRQ_HANDLED;
  743. }
  744. static struct videobuf_queue_ops omap1_videobuf_ops = {
  745. .buf_setup = omap1_videobuf_setup,
  746. .buf_prepare = omap1_videobuf_prepare,
  747. .buf_queue = omap1_videobuf_queue,
  748. .buf_release = omap1_videobuf_release,
  749. };
  750. /*
  751. * SOC Camera host operations
  752. */
  753. static void sensor_reset(struct omap1_cam_dev *pcdev, bool reset)
  754. {
  755. /* apply/release camera sensor reset if requested by platform data */
  756. if (pcdev->pflags & OMAP1_CAMERA_RST_HIGH)
  757. CAM_WRITE(pcdev, GPIO, reset);
  758. else if (pcdev->pflags & OMAP1_CAMERA_RST_LOW)
  759. CAM_WRITE(pcdev, GPIO, !reset);
  760. }
  761. /*
  762. * The following two functions absolutely depend on the fact, that
  763. * there can be only one camera on OMAP1 camera sensor interface
  764. */
  765. static int omap1_cam_add_device(struct soc_camera_device *icd)
  766. {
  767. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  768. struct omap1_cam_dev *pcdev = ici->priv;
  769. u32 ctrlclock;
  770. if (pcdev->icd)
  771. return -EBUSY;
  772. clk_enable(pcdev->clk);
  773. /* setup sensor clock */
  774. ctrlclock = CAM_READ(pcdev, CTRLCLOCK);
  775. ctrlclock &= ~(CAMEXCLK_EN | MCLK_EN | DPLL_EN);
  776. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  777. ctrlclock &= ~FOSCMOD_MASK;
  778. switch (pcdev->camexclk) {
  779. case 6000000:
  780. ctrlclock |= CAMEXCLK_EN | FOSCMOD_6MHz;
  781. break;
  782. case 8000000:
  783. ctrlclock |= CAMEXCLK_EN | FOSCMOD_8MHz | DPLL_EN;
  784. break;
  785. case 9600000:
  786. ctrlclock |= CAMEXCLK_EN | FOSCMOD_9_6MHz | DPLL_EN;
  787. break;
  788. case 12000000:
  789. ctrlclock |= CAMEXCLK_EN | FOSCMOD_12MHz;
  790. break;
  791. case 24000000:
  792. ctrlclock |= CAMEXCLK_EN | FOSCMOD_24MHz | DPLL_EN;
  793. default:
  794. break;
  795. }
  796. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~DPLL_EN);
  797. /* enable internal clock */
  798. ctrlclock |= MCLK_EN;
  799. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  800. sensor_reset(pcdev, false);
  801. pcdev->icd = icd;
  802. dev_dbg(icd->parent, "OMAP1 Camera driver attached to camera %d\n",
  803. icd->devnum);
  804. return 0;
  805. }
  806. static void omap1_cam_remove_device(struct soc_camera_device *icd)
  807. {
  808. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  809. struct omap1_cam_dev *pcdev = ici->priv;
  810. u32 ctrlclock;
  811. BUG_ON(icd != pcdev->icd);
  812. suspend_capture(pcdev);
  813. disable_capture(pcdev);
  814. sensor_reset(pcdev, true);
  815. /* disable and release system clocks */
  816. ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  817. ctrlclock &= ~(MCLK_EN | DPLL_EN | CAMEXCLK_EN);
  818. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  819. ctrlclock = (ctrlclock & ~FOSCMOD_MASK) | FOSCMOD_12MHz;
  820. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  821. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | MCLK_EN);
  822. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~MCLK_EN);
  823. clk_disable(pcdev->clk);
  824. pcdev->icd = NULL;
  825. dev_dbg(icd->parent,
  826. "OMAP1 Camera driver detached from camera %d\n", icd->devnum);
  827. }
  828. /* Duplicate standard formats based on host capability of byte swapping */
  829. static const struct soc_mbus_lookup omap1_cam_formats[] = {
  830. {
  831. .code = V4L2_MBUS_FMT_UYVY8_2X8,
  832. .fmt = {
  833. .fourcc = V4L2_PIX_FMT_YUYV,
  834. .name = "YUYV",
  835. .bits_per_sample = 8,
  836. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  837. .order = SOC_MBUS_ORDER_BE,
  838. },
  839. }, {
  840. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  841. .fmt = {
  842. .fourcc = V4L2_PIX_FMT_YVYU,
  843. .name = "YVYU",
  844. .bits_per_sample = 8,
  845. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  846. .order = SOC_MBUS_ORDER_BE,
  847. },
  848. }, {
  849. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  850. .fmt = {
  851. .fourcc = V4L2_PIX_FMT_UYVY,
  852. .name = "UYVY",
  853. .bits_per_sample = 8,
  854. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  855. .order = SOC_MBUS_ORDER_BE,
  856. },
  857. }, {
  858. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  859. .fmt = {
  860. .fourcc = V4L2_PIX_FMT_VYUY,
  861. .name = "VYUY",
  862. .bits_per_sample = 8,
  863. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  864. .order = SOC_MBUS_ORDER_BE,
  865. },
  866. }, {
  867. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE,
  868. .fmt = {
  869. .fourcc = V4L2_PIX_FMT_RGB555,
  870. .name = "RGB555",
  871. .bits_per_sample = 8,
  872. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  873. .order = SOC_MBUS_ORDER_BE,
  874. },
  875. }, {
  876. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  877. .fmt = {
  878. .fourcc = V4L2_PIX_FMT_RGB555X,
  879. .name = "RGB555X",
  880. .bits_per_sample = 8,
  881. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  882. .order = SOC_MBUS_ORDER_BE,
  883. },
  884. }, {
  885. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  886. .fmt = {
  887. .fourcc = V4L2_PIX_FMT_RGB565,
  888. .name = "RGB565",
  889. .bits_per_sample = 8,
  890. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  891. .order = SOC_MBUS_ORDER_BE,
  892. },
  893. }, {
  894. .code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  895. .fmt = {
  896. .fourcc = V4L2_PIX_FMT_RGB565X,
  897. .name = "RGB565X",
  898. .bits_per_sample = 8,
  899. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  900. .order = SOC_MBUS_ORDER_BE,
  901. },
  902. },
  903. };
  904. static int omap1_cam_get_formats(struct soc_camera_device *icd,
  905. unsigned int idx, struct soc_camera_format_xlate *xlate)
  906. {
  907. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  908. struct device *dev = icd->parent;
  909. int formats = 0, ret;
  910. enum v4l2_mbus_pixelcode code;
  911. const struct soc_mbus_pixelfmt *fmt;
  912. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  913. if (ret < 0)
  914. /* No more formats */
  915. return 0;
  916. fmt = soc_mbus_get_fmtdesc(code);
  917. if (!fmt) {
  918. dev_warn(dev, "%s: unsupported format code #%d: %d\n", __func__,
  919. idx, code);
  920. return 0;
  921. }
  922. /* Check support for the requested bits-per-sample */
  923. if (fmt->bits_per_sample != 8)
  924. return 0;
  925. switch (code) {
  926. case V4L2_MBUS_FMT_YUYV8_2X8:
  927. case V4L2_MBUS_FMT_YVYU8_2X8:
  928. case V4L2_MBUS_FMT_UYVY8_2X8:
  929. case V4L2_MBUS_FMT_VYUY8_2X8:
  930. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
  931. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  932. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  933. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  934. formats++;
  935. if (xlate) {
  936. xlate->host_fmt = soc_mbus_find_fmtdesc(code,
  937. omap1_cam_formats,
  938. ARRAY_SIZE(omap1_cam_formats));
  939. xlate->code = code;
  940. xlate++;
  941. dev_dbg(dev,
  942. "%s: providing format %s as byte swapped code #%d\n",
  943. __func__, xlate->host_fmt->name, code);
  944. }
  945. default:
  946. if (xlate)
  947. dev_dbg(dev,
  948. "%s: providing format %s in pass-through mode\n",
  949. __func__, fmt->name);
  950. }
  951. formats++;
  952. if (xlate) {
  953. xlate->host_fmt = fmt;
  954. xlate->code = code;
  955. xlate++;
  956. }
  957. return formats;
  958. }
  959. static bool is_dma_aligned(s32 bytes_per_line, unsigned int height,
  960. enum omap1_cam_vb_mode vb_mode)
  961. {
  962. int size = bytes_per_line * height;
  963. return IS_ALIGNED(bytes_per_line, DMA_ELEMENT_SIZE) &&
  964. IS_ALIGNED(size, DMA_FRAME_SIZE(vb_mode) * DMA_ELEMENT_SIZE);
  965. }
  966. static int dma_align(int *width, int *height,
  967. const struct soc_mbus_pixelfmt *fmt,
  968. enum omap1_cam_vb_mode vb_mode, bool enlarge)
  969. {
  970. s32 bytes_per_line = soc_mbus_bytes_per_line(*width, fmt);
  971. if (bytes_per_line < 0)
  972. return bytes_per_line;
  973. if (!is_dma_aligned(bytes_per_line, *height, vb_mode)) {
  974. unsigned int pxalign = __fls(bytes_per_line / *width);
  975. unsigned int salign = DMA_FRAME_SHIFT(vb_mode) +
  976. DMA_ELEMENT_SHIFT - pxalign;
  977. unsigned int incr = enlarge << salign;
  978. v4l_bound_align_image(width, 1, *width + incr, 0,
  979. height, 1, *height + incr, 0, salign);
  980. return 0;
  981. }
  982. return 1;
  983. }
  984. #define subdev_call_with_sense(pcdev, dev, icd, sd, function, args...) \
  985. ({ \
  986. struct soc_camera_sense sense = { \
  987. .master_clock = pcdev->camexclk, \
  988. .pixel_clock_max = 0, \
  989. }; \
  990. int __ret; \
  991. \
  992. if (pcdev->pdata) \
  993. sense.pixel_clock_max = pcdev->pdata->lclk_khz_max * 1000; \
  994. icd->sense = &sense; \
  995. __ret = v4l2_subdev_call(sd, video, function, ##args); \
  996. icd->sense = NULL; \
  997. \
  998. if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { \
  999. if (sense.pixel_clock > sense.pixel_clock_max) { \
  1000. dev_err(dev, \
  1001. "%s: pixel clock %lu set by the camera too high!\n", \
  1002. __func__, sense.pixel_clock); \
  1003. __ret = -EINVAL; \
  1004. } \
  1005. } \
  1006. __ret; \
  1007. })
  1008. static int set_mbus_format(struct omap1_cam_dev *pcdev, struct device *dev,
  1009. struct soc_camera_device *icd, struct v4l2_subdev *sd,
  1010. struct v4l2_mbus_framefmt *mf,
  1011. const struct soc_camera_format_xlate *xlate)
  1012. {
  1013. s32 bytes_per_line;
  1014. int ret = subdev_call_with_sense(pcdev, dev, icd, sd, s_mbus_fmt, mf);
  1015. if (ret < 0) {
  1016. dev_err(dev, "%s: s_mbus_fmt failed\n", __func__);
  1017. return ret;
  1018. }
  1019. if (mf->code != xlate->code) {
  1020. dev_err(dev, "%s: unexpected pixel code change\n", __func__);
  1021. return -EINVAL;
  1022. }
  1023. bytes_per_line = soc_mbus_bytes_per_line(mf->width, xlate->host_fmt);
  1024. if (bytes_per_line < 0) {
  1025. dev_err(dev, "%s: soc_mbus_bytes_per_line() failed\n",
  1026. __func__);
  1027. return bytes_per_line;
  1028. }
  1029. if (!is_dma_aligned(bytes_per_line, mf->height, pcdev->vb_mode)) {
  1030. dev_err(dev, "%s: resulting geometry %ux%u not DMA aligned\n",
  1031. __func__, mf->width, mf->height);
  1032. return -EINVAL;
  1033. }
  1034. return 0;
  1035. }
  1036. static int omap1_cam_set_crop(struct soc_camera_device *icd,
  1037. struct v4l2_crop *crop)
  1038. {
  1039. struct v4l2_rect *rect = &crop->c;
  1040. const struct soc_camera_format_xlate *xlate = icd->current_fmt;
  1041. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1042. struct device *dev = icd->parent;
  1043. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1044. struct omap1_cam_dev *pcdev = ici->priv;
  1045. struct v4l2_mbus_framefmt mf;
  1046. int ret;
  1047. ret = subdev_call_with_sense(pcdev, dev, icd, sd, s_crop, crop);
  1048. if (ret < 0) {
  1049. dev_warn(dev, "%s: failed to crop to %ux%u@%u:%u\n", __func__,
  1050. rect->width, rect->height, rect->left, rect->top);
  1051. return ret;
  1052. }
  1053. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  1054. if (ret < 0) {
  1055. dev_warn(dev, "%s: failed to fetch current format\n", __func__);
  1056. return ret;
  1057. }
  1058. ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->vb_mode,
  1059. false);
  1060. if (ret < 0) {
  1061. dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
  1062. __func__, mf.width, mf.height,
  1063. xlate->host_fmt->name);
  1064. return ret;
  1065. }
  1066. if (!ret) {
  1067. /* sensor returned geometry not DMA aligned, trying to fix */
  1068. ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
  1069. if (ret < 0) {
  1070. dev_err(dev, "%s: failed to set format\n", __func__);
  1071. return ret;
  1072. }
  1073. }
  1074. icd->user_width = mf.width;
  1075. icd->user_height = mf.height;
  1076. return 0;
  1077. }
  1078. static int omap1_cam_set_fmt(struct soc_camera_device *icd,
  1079. struct v4l2_format *f)
  1080. {
  1081. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1082. const struct soc_camera_format_xlate *xlate;
  1083. struct device *dev = icd->parent;
  1084. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1085. struct omap1_cam_dev *pcdev = ici->priv;
  1086. struct v4l2_pix_format *pix = &f->fmt.pix;
  1087. struct v4l2_mbus_framefmt mf;
  1088. int ret;
  1089. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1090. if (!xlate) {
  1091. dev_warn(dev, "%s: format %#x not found\n", __func__,
  1092. pix->pixelformat);
  1093. return -EINVAL;
  1094. }
  1095. mf.width = pix->width;
  1096. mf.height = pix->height;
  1097. mf.field = pix->field;
  1098. mf.colorspace = pix->colorspace;
  1099. mf.code = xlate->code;
  1100. ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->vb_mode,
  1101. true);
  1102. if (ret < 0) {
  1103. dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
  1104. __func__, pix->width, pix->height,
  1105. xlate->host_fmt->name);
  1106. return ret;
  1107. }
  1108. ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
  1109. if (ret < 0) {
  1110. dev_err(dev, "%s: failed to set format\n", __func__);
  1111. return ret;
  1112. }
  1113. pix->width = mf.width;
  1114. pix->height = mf.height;
  1115. pix->field = mf.field;
  1116. pix->colorspace = mf.colorspace;
  1117. icd->current_fmt = xlate;
  1118. return 0;
  1119. }
  1120. static int omap1_cam_try_fmt(struct soc_camera_device *icd,
  1121. struct v4l2_format *f)
  1122. {
  1123. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1124. const struct soc_camera_format_xlate *xlate;
  1125. struct v4l2_pix_format *pix = &f->fmt.pix;
  1126. struct v4l2_mbus_framefmt mf;
  1127. int ret;
  1128. /* TODO: limit to mx1 hardware capabilities */
  1129. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1130. if (!xlate) {
  1131. dev_warn(icd->parent, "Format %#x not found\n",
  1132. pix->pixelformat);
  1133. return -EINVAL;
  1134. }
  1135. mf.width = pix->width;
  1136. mf.height = pix->height;
  1137. mf.field = pix->field;
  1138. mf.colorspace = pix->colorspace;
  1139. mf.code = xlate->code;
  1140. /* limit to sensor capabilities */
  1141. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1142. if (ret < 0)
  1143. return ret;
  1144. pix->width = mf.width;
  1145. pix->height = mf.height;
  1146. pix->field = mf.field;
  1147. pix->colorspace = mf.colorspace;
  1148. return 0;
  1149. }
  1150. static bool sg_mode;
  1151. /*
  1152. * Local mmap_mapper wrapper,
  1153. * used for detecting videobuf-dma-contig buffer allocation failures
  1154. * and switching to videobuf-dma-sg automatically for future attempts.
  1155. */
  1156. static int omap1_cam_mmap_mapper(struct videobuf_queue *q,
  1157. struct videobuf_buffer *buf,
  1158. struct vm_area_struct *vma)
  1159. {
  1160. struct soc_camera_device *icd = q->priv_data;
  1161. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1162. struct omap1_cam_dev *pcdev = ici->priv;
  1163. int ret;
  1164. ret = pcdev->mmap_mapper(q, buf, vma);
  1165. if (ret == -ENOMEM)
  1166. sg_mode = true;
  1167. return ret;
  1168. }
  1169. static void omap1_cam_init_videobuf(struct videobuf_queue *q,
  1170. struct soc_camera_device *icd)
  1171. {
  1172. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1173. struct omap1_cam_dev *pcdev = ici->priv;
  1174. if (!sg_mode)
  1175. videobuf_queue_dma_contig_init(q, &omap1_videobuf_ops,
  1176. icd->parent, &pcdev->lock,
  1177. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  1178. sizeof(struct omap1_cam_buf), icd, &icd->video_lock);
  1179. else
  1180. videobuf_queue_sg_init(q, &omap1_videobuf_ops,
  1181. icd->parent, &pcdev->lock,
  1182. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  1183. sizeof(struct omap1_cam_buf), icd, &icd->video_lock);
  1184. /* use videobuf mode (auto)selected with the module parameter */
  1185. pcdev->vb_mode = sg_mode ? OMAP1_CAM_DMA_SG : OMAP1_CAM_DMA_CONTIG;
  1186. /*
  1187. * Ensure we substitute the videobuf-dma-contig version of the
  1188. * mmap_mapper() callback with our own wrapper, used for switching
  1189. * automatically to videobuf-dma-sg on buffer allocation failure.
  1190. */
  1191. if (!sg_mode && q->int_ops->mmap_mapper != omap1_cam_mmap_mapper) {
  1192. pcdev->mmap_mapper = q->int_ops->mmap_mapper;
  1193. q->int_ops->mmap_mapper = omap1_cam_mmap_mapper;
  1194. }
  1195. }
  1196. static int omap1_cam_reqbufs(struct soc_camera_device *icd,
  1197. struct v4l2_requestbuffers *p)
  1198. {
  1199. int i;
  1200. /*
  1201. * This is for locking debugging only. I removed spinlocks and now I
  1202. * check whether .prepare is ever called on a linked buffer, or whether
  1203. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1204. * it hadn't triggered
  1205. */
  1206. for (i = 0; i < p->count; i++) {
  1207. struct omap1_cam_buf *buf = container_of(icd->vb_vidq.bufs[i],
  1208. struct omap1_cam_buf, vb);
  1209. buf->inwork = 0;
  1210. INIT_LIST_HEAD(&buf->vb.queue);
  1211. }
  1212. return 0;
  1213. }
  1214. static int omap1_cam_querycap(struct soc_camera_host *ici,
  1215. struct v4l2_capability *cap)
  1216. {
  1217. /* cap->name is set by the friendly caller:-> */
  1218. strlcpy(cap->card, "OMAP1 Camera", sizeof(cap->card));
  1219. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1220. return 0;
  1221. }
  1222. static int omap1_cam_set_bus_param(struct soc_camera_device *icd)
  1223. {
  1224. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1225. struct device *dev = icd->parent;
  1226. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1227. struct omap1_cam_dev *pcdev = ici->priv;
  1228. u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
  1229. const struct soc_camera_format_xlate *xlate;
  1230. const struct soc_mbus_pixelfmt *fmt;
  1231. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1232. unsigned long common_flags;
  1233. u32 ctrlclock, mode;
  1234. int ret;
  1235. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  1236. if (!ret) {
  1237. common_flags = soc_mbus_config_compatible(&cfg, SOCAM_BUS_FLAGS);
  1238. if (!common_flags) {
  1239. dev_warn(dev,
  1240. "Flags incompatible: camera 0x%x, host 0x%x\n",
  1241. cfg.flags, SOCAM_BUS_FLAGS);
  1242. return -EINVAL;
  1243. }
  1244. } else if (ret != -ENOIOCTLCMD) {
  1245. return ret;
  1246. } else {
  1247. common_flags = SOCAM_BUS_FLAGS;
  1248. }
  1249. /* Make choices, possibly based on platform configuration */
  1250. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  1251. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  1252. if (!pcdev->pdata ||
  1253. pcdev->pdata->flags & OMAP1_CAMERA_LCLK_RISING)
  1254. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1255. else
  1256. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  1257. }
  1258. cfg.flags = common_flags;
  1259. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  1260. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1261. dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
  1262. common_flags, ret);
  1263. return ret;
  1264. }
  1265. ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  1266. if (ctrlclock & LCLK_EN)
  1267. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  1268. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) {
  1269. dev_dbg(dev, "CTRLCLOCK_REG |= POLCLK\n");
  1270. ctrlclock |= POLCLK;
  1271. } else {
  1272. dev_dbg(dev, "CTRLCLOCK_REG &= ~POLCLK\n");
  1273. ctrlclock &= ~POLCLK;
  1274. }
  1275. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  1276. if (ctrlclock & LCLK_EN)
  1277. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  1278. /* select bus endianess */
  1279. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1280. fmt = xlate->host_fmt;
  1281. mode = CAM_READ(pcdev, MODE) & ~(RAZ_FIFO | IRQ_MASK | DMA);
  1282. if (fmt->order == SOC_MBUS_ORDER_LE) {
  1283. dev_dbg(dev, "MODE_REG &= ~ORDERCAMD\n");
  1284. CAM_WRITE(pcdev, MODE, mode & ~ORDERCAMD);
  1285. } else {
  1286. dev_dbg(dev, "MODE_REG |= ORDERCAMD\n");
  1287. CAM_WRITE(pcdev, MODE, mode | ORDERCAMD);
  1288. }
  1289. return 0;
  1290. }
  1291. static unsigned int omap1_cam_poll(struct file *file, poll_table *pt)
  1292. {
  1293. struct soc_camera_device *icd = file->private_data;
  1294. struct omap1_cam_buf *buf;
  1295. buf = list_entry(icd->vb_vidq.stream.next, struct omap1_cam_buf,
  1296. vb.stream);
  1297. poll_wait(file, &buf->vb.done, pt);
  1298. if (buf->vb.state == VIDEOBUF_DONE ||
  1299. buf->vb.state == VIDEOBUF_ERROR)
  1300. return POLLIN | POLLRDNORM;
  1301. return 0;
  1302. }
  1303. static struct soc_camera_host_ops omap1_host_ops = {
  1304. .owner = THIS_MODULE,
  1305. .add = omap1_cam_add_device,
  1306. .remove = omap1_cam_remove_device,
  1307. .get_formats = omap1_cam_get_formats,
  1308. .set_crop = omap1_cam_set_crop,
  1309. .set_fmt = omap1_cam_set_fmt,
  1310. .try_fmt = omap1_cam_try_fmt,
  1311. .init_videobuf = omap1_cam_init_videobuf,
  1312. .reqbufs = omap1_cam_reqbufs,
  1313. .querycap = omap1_cam_querycap,
  1314. .set_bus_param = omap1_cam_set_bus_param,
  1315. .poll = omap1_cam_poll,
  1316. };
  1317. static int __init omap1_cam_probe(struct platform_device *pdev)
  1318. {
  1319. struct omap1_cam_dev *pcdev;
  1320. struct resource *res;
  1321. struct clk *clk;
  1322. void __iomem *base;
  1323. unsigned int irq;
  1324. int err = 0;
  1325. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1326. irq = platform_get_irq(pdev, 0);
  1327. if (!res || (int)irq <= 0) {
  1328. err = -ENODEV;
  1329. goto exit;
  1330. }
  1331. clk = clk_get(&pdev->dev, "armper_ck");
  1332. if (IS_ERR(clk)) {
  1333. err = PTR_ERR(clk);
  1334. goto exit;
  1335. }
  1336. pcdev = kzalloc(sizeof(*pcdev) + resource_size(res), GFP_KERNEL);
  1337. if (!pcdev) {
  1338. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1339. err = -ENOMEM;
  1340. goto exit_put_clk;
  1341. }
  1342. pcdev->res = res;
  1343. pcdev->clk = clk;
  1344. pcdev->pdata = pdev->dev.platform_data;
  1345. if (pcdev->pdata) {
  1346. pcdev->pflags = pcdev->pdata->flags;
  1347. pcdev->camexclk = pcdev->pdata->camexclk_khz * 1000;
  1348. }
  1349. switch (pcdev->camexclk) {
  1350. case 6000000:
  1351. case 8000000:
  1352. case 9600000:
  1353. case 12000000:
  1354. case 24000000:
  1355. break;
  1356. default:
  1357. /* pcdev->camexclk != 0 => pcdev->pdata != NULL */
  1358. dev_warn(&pdev->dev,
  1359. "Incorrect sensor clock frequency %ld kHz, "
  1360. "should be one of 0, 6, 8, 9.6, 12 or 24 MHz, "
  1361. "please correct your platform data\n",
  1362. pcdev->pdata->camexclk_khz);
  1363. pcdev->camexclk = 0;
  1364. case 0:
  1365. dev_info(&pdev->dev, "Not providing sensor clock\n");
  1366. }
  1367. INIT_LIST_HEAD(&pcdev->capture);
  1368. spin_lock_init(&pcdev->lock);
  1369. /*
  1370. * Request the region.
  1371. */
  1372. if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
  1373. err = -EBUSY;
  1374. goto exit_kfree;
  1375. }
  1376. base = ioremap(res->start, resource_size(res));
  1377. if (!base) {
  1378. err = -ENOMEM;
  1379. goto exit_release;
  1380. }
  1381. pcdev->irq = irq;
  1382. pcdev->base = base;
  1383. sensor_reset(pcdev, true);
  1384. err = omap_request_dma(OMAP_DMA_CAMERA_IF_RX, DRIVER_NAME,
  1385. dma_isr, (void *)pcdev, &pcdev->dma_ch);
  1386. if (err < 0) {
  1387. dev_err(&pdev->dev, "Can't request DMA for OMAP1 Camera\n");
  1388. err = -EBUSY;
  1389. goto exit_iounmap;
  1390. }
  1391. dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_ch);
  1392. /* preconfigure DMA */
  1393. omap_set_dma_src_params(pcdev->dma_ch, OMAP_DMA_PORT_TIPB,
  1394. OMAP_DMA_AMODE_CONSTANT, res->start + REG_CAMDATA,
  1395. 0, 0);
  1396. omap_set_dma_dest_burst_mode(pcdev->dma_ch, OMAP_DMA_DATA_BURST_4);
  1397. /* setup DMA autoinitialization */
  1398. omap_dma_link_lch(pcdev->dma_ch, pcdev->dma_ch);
  1399. err = request_irq(pcdev->irq, cam_isr, 0, DRIVER_NAME, pcdev);
  1400. if (err) {
  1401. dev_err(&pdev->dev, "Camera interrupt register failed\n");
  1402. goto exit_free_dma;
  1403. }
  1404. pcdev->soc_host.drv_name = DRIVER_NAME;
  1405. pcdev->soc_host.ops = &omap1_host_ops;
  1406. pcdev->soc_host.priv = pcdev;
  1407. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1408. pcdev->soc_host.nr = pdev->id;
  1409. err = soc_camera_host_register(&pcdev->soc_host);
  1410. if (err)
  1411. goto exit_free_irq;
  1412. dev_info(&pdev->dev, "OMAP1 Camera Interface driver loaded\n");
  1413. return 0;
  1414. exit_free_irq:
  1415. free_irq(pcdev->irq, pcdev);
  1416. exit_free_dma:
  1417. omap_free_dma(pcdev->dma_ch);
  1418. exit_iounmap:
  1419. iounmap(base);
  1420. exit_release:
  1421. release_mem_region(res->start, resource_size(res));
  1422. exit_kfree:
  1423. kfree(pcdev);
  1424. exit_put_clk:
  1425. clk_put(clk);
  1426. exit:
  1427. return err;
  1428. }
  1429. static int __exit omap1_cam_remove(struct platform_device *pdev)
  1430. {
  1431. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1432. struct omap1_cam_dev *pcdev = container_of(soc_host,
  1433. struct omap1_cam_dev, soc_host);
  1434. struct resource *res;
  1435. free_irq(pcdev->irq, pcdev);
  1436. omap_free_dma(pcdev->dma_ch);
  1437. soc_camera_host_unregister(soc_host);
  1438. iounmap(pcdev->base);
  1439. res = pcdev->res;
  1440. release_mem_region(res->start, resource_size(res));
  1441. clk_put(pcdev->clk);
  1442. kfree(pcdev);
  1443. dev_info(&pdev->dev, "OMAP1 Camera Interface driver unloaded\n");
  1444. return 0;
  1445. }
  1446. static struct platform_driver omap1_cam_driver = {
  1447. .driver = {
  1448. .name = DRIVER_NAME,
  1449. },
  1450. .probe = omap1_cam_probe,
  1451. .remove = __exit_p(omap1_cam_remove),
  1452. };
  1453. module_platform_driver(omap1_cam_driver);
  1454. module_param(sg_mode, bool, 0644);
  1455. MODULE_PARM_DESC(sg_mode, "videobuf mode, 0: dma-contig (default), 1: dma-sg");
  1456. MODULE_DESCRIPTION("OMAP1 Camera Interface driver");
  1457. MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
  1458. MODULE_LICENSE("GPL v2");
  1459. MODULE_VERSION(DRIVER_VERSION);
  1460. MODULE_ALIAS("platform:" DRIVER_NAME);