omap1_camera.c 44 KB

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