omap1_camera.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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. .layout = SOC_MBUS_LAYOUT_PACKED,
  839. },
  840. }, {
  841. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  842. .fmt = {
  843. .fourcc = V4L2_PIX_FMT_YVYU,
  844. .name = "YVYU",
  845. .bits_per_sample = 8,
  846. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  847. .order = SOC_MBUS_ORDER_BE,
  848. .layout = SOC_MBUS_LAYOUT_PACKED,
  849. },
  850. }, {
  851. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  852. .fmt = {
  853. .fourcc = V4L2_PIX_FMT_UYVY,
  854. .name = "UYVY",
  855. .bits_per_sample = 8,
  856. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  857. .order = SOC_MBUS_ORDER_BE,
  858. .layout = SOC_MBUS_LAYOUT_PACKED,
  859. },
  860. }, {
  861. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  862. .fmt = {
  863. .fourcc = V4L2_PIX_FMT_VYUY,
  864. .name = "VYUY",
  865. .bits_per_sample = 8,
  866. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  867. .order = SOC_MBUS_ORDER_BE,
  868. .layout = SOC_MBUS_LAYOUT_PACKED,
  869. },
  870. }, {
  871. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE,
  872. .fmt = {
  873. .fourcc = V4L2_PIX_FMT_RGB555,
  874. .name = "RGB555",
  875. .bits_per_sample = 8,
  876. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  877. .order = SOC_MBUS_ORDER_BE,
  878. .layout = SOC_MBUS_LAYOUT_PACKED,
  879. },
  880. }, {
  881. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  882. .fmt = {
  883. .fourcc = V4L2_PIX_FMT_RGB555X,
  884. .name = "RGB555X",
  885. .bits_per_sample = 8,
  886. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  887. .order = SOC_MBUS_ORDER_BE,
  888. .layout = SOC_MBUS_LAYOUT_PACKED,
  889. },
  890. }, {
  891. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  892. .fmt = {
  893. .fourcc = V4L2_PIX_FMT_RGB565,
  894. .name = "RGB565",
  895. .bits_per_sample = 8,
  896. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  897. .order = SOC_MBUS_ORDER_BE,
  898. .layout = SOC_MBUS_LAYOUT_PACKED,
  899. },
  900. }, {
  901. .code = V4L2_MBUS_FMT_RGB565_2X8_LE,
  902. .fmt = {
  903. .fourcc = V4L2_PIX_FMT_RGB565X,
  904. .name = "RGB565X",
  905. .bits_per_sample = 8,
  906. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  907. .order = SOC_MBUS_ORDER_BE,
  908. .layout = SOC_MBUS_LAYOUT_PACKED,
  909. },
  910. },
  911. };
  912. static int omap1_cam_get_formats(struct soc_camera_device *icd,
  913. unsigned int idx, struct soc_camera_format_xlate *xlate)
  914. {
  915. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  916. struct device *dev = icd->parent;
  917. int formats = 0, ret;
  918. enum v4l2_mbus_pixelcode code;
  919. const struct soc_mbus_pixelfmt *fmt;
  920. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  921. if (ret < 0)
  922. /* No more formats */
  923. return 0;
  924. fmt = soc_mbus_get_fmtdesc(code);
  925. if (!fmt) {
  926. dev_warn(dev, "%s: unsupported format code #%d: %d\n", __func__,
  927. idx, code);
  928. return 0;
  929. }
  930. /* Check support for the requested bits-per-sample */
  931. if (fmt->bits_per_sample != 8)
  932. return 0;
  933. switch (code) {
  934. case V4L2_MBUS_FMT_YUYV8_2X8:
  935. case V4L2_MBUS_FMT_YVYU8_2X8:
  936. case V4L2_MBUS_FMT_UYVY8_2X8:
  937. case V4L2_MBUS_FMT_VYUY8_2X8:
  938. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE:
  939. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  940. case V4L2_MBUS_FMT_RGB565_2X8_BE:
  941. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  942. formats++;
  943. if (xlate) {
  944. xlate->host_fmt = soc_mbus_find_fmtdesc(code,
  945. omap1_cam_formats,
  946. ARRAY_SIZE(omap1_cam_formats));
  947. xlate->code = code;
  948. xlate++;
  949. dev_dbg(dev,
  950. "%s: providing format %s as byte swapped code #%d\n",
  951. __func__, xlate->host_fmt->name, code);
  952. }
  953. default:
  954. if (xlate)
  955. dev_dbg(dev,
  956. "%s: providing format %s in pass-through mode\n",
  957. __func__, fmt->name);
  958. }
  959. formats++;
  960. if (xlate) {
  961. xlate->host_fmt = fmt;
  962. xlate->code = code;
  963. xlate++;
  964. }
  965. return formats;
  966. }
  967. static bool is_dma_aligned(s32 bytes_per_line, unsigned int height,
  968. enum omap1_cam_vb_mode vb_mode)
  969. {
  970. int size = bytes_per_line * height;
  971. return IS_ALIGNED(bytes_per_line, DMA_ELEMENT_SIZE) &&
  972. IS_ALIGNED(size, DMA_FRAME_SIZE(vb_mode) * DMA_ELEMENT_SIZE);
  973. }
  974. static int dma_align(int *width, int *height,
  975. const struct soc_mbus_pixelfmt *fmt,
  976. enum omap1_cam_vb_mode vb_mode, bool enlarge)
  977. {
  978. s32 bytes_per_line = soc_mbus_bytes_per_line(*width, fmt);
  979. if (bytes_per_line < 0)
  980. return bytes_per_line;
  981. if (!is_dma_aligned(bytes_per_line, *height, vb_mode)) {
  982. unsigned int pxalign = __fls(bytes_per_line / *width);
  983. unsigned int salign = DMA_FRAME_SHIFT(vb_mode) +
  984. DMA_ELEMENT_SHIFT - pxalign;
  985. unsigned int incr = enlarge << salign;
  986. v4l_bound_align_image(width, 1, *width + incr, 0,
  987. height, 1, *height + incr, 0, salign);
  988. return 0;
  989. }
  990. return 1;
  991. }
  992. #define subdev_call_with_sense(pcdev, dev, icd, sd, function, args...) \
  993. ({ \
  994. struct soc_camera_sense sense = { \
  995. .master_clock = pcdev->camexclk, \
  996. .pixel_clock_max = 0, \
  997. }; \
  998. int __ret; \
  999. \
  1000. if (pcdev->pdata) \
  1001. sense.pixel_clock_max = pcdev->pdata->lclk_khz_max * 1000; \
  1002. icd->sense = &sense; \
  1003. __ret = v4l2_subdev_call(sd, video, function, ##args); \
  1004. icd->sense = NULL; \
  1005. \
  1006. if (sense.flags & SOCAM_SENSE_PCLK_CHANGED) { \
  1007. if (sense.pixel_clock > sense.pixel_clock_max) { \
  1008. dev_err(dev, \
  1009. "%s: pixel clock %lu set by the camera too high!\n", \
  1010. __func__, sense.pixel_clock); \
  1011. __ret = -EINVAL; \
  1012. } \
  1013. } \
  1014. __ret; \
  1015. })
  1016. static int set_mbus_format(struct omap1_cam_dev *pcdev, struct device *dev,
  1017. struct soc_camera_device *icd, struct v4l2_subdev *sd,
  1018. struct v4l2_mbus_framefmt *mf,
  1019. const struct soc_camera_format_xlate *xlate)
  1020. {
  1021. s32 bytes_per_line;
  1022. int ret = subdev_call_with_sense(pcdev, dev, icd, sd, s_mbus_fmt, mf);
  1023. if (ret < 0) {
  1024. dev_err(dev, "%s: s_mbus_fmt failed\n", __func__);
  1025. return ret;
  1026. }
  1027. if (mf->code != xlate->code) {
  1028. dev_err(dev, "%s: unexpected pixel code change\n", __func__);
  1029. return -EINVAL;
  1030. }
  1031. bytes_per_line = soc_mbus_bytes_per_line(mf->width, xlate->host_fmt);
  1032. if (bytes_per_line < 0) {
  1033. dev_err(dev, "%s: soc_mbus_bytes_per_line() failed\n",
  1034. __func__);
  1035. return bytes_per_line;
  1036. }
  1037. if (!is_dma_aligned(bytes_per_line, mf->height, pcdev->vb_mode)) {
  1038. dev_err(dev, "%s: resulting geometry %ux%u not DMA aligned\n",
  1039. __func__, mf->width, mf->height);
  1040. return -EINVAL;
  1041. }
  1042. return 0;
  1043. }
  1044. static int omap1_cam_set_crop(struct soc_camera_device *icd,
  1045. struct v4l2_crop *crop)
  1046. {
  1047. struct v4l2_rect *rect = &crop->c;
  1048. const struct soc_camera_format_xlate *xlate = icd->current_fmt;
  1049. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1050. struct device *dev = icd->parent;
  1051. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1052. struct omap1_cam_dev *pcdev = ici->priv;
  1053. struct v4l2_mbus_framefmt mf;
  1054. int ret;
  1055. ret = subdev_call_with_sense(pcdev, dev, icd, sd, s_crop, crop);
  1056. if (ret < 0) {
  1057. dev_warn(dev, "%s: failed to crop to %ux%u@%u:%u\n", __func__,
  1058. rect->width, rect->height, rect->left, rect->top);
  1059. return ret;
  1060. }
  1061. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  1062. if (ret < 0) {
  1063. dev_warn(dev, "%s: failed to fetch current format\n", __func__);
  1064. return ret;
  1065. }
  1066. ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->vb_mode,
  1067. false);
  1068. if (ret < 0) {
  1069. dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
  1070. __func__, mf.width, mf.height,
  1071. xlate->host_fmt->name);
  1072. return ret;
  1073. }
  1074. if (!ret) {
  1075. /* sensor returned geometry not DMA aligned, trying to fix */
  1076. ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
  1077. if (ret < 0) {
  1078. dev_err(dev, "%s: failed to set format\n", __func__);
  1079. return ret;
  1080. }
  1081. }
  1082. icd->user_width = mf.width;
  1083. icd->user_height = mf.height;
  1084. return 0;
  1085. }
  1086. static int omap1_cam_set_fmt(struct soc_camera_device *icd,
  1087. struct v4l2_format *f)
  1088. {
  1089. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1090. const struct soc_camera_format_xlate *xlate;
  1091. struct device *dev = icd->parent;
  1092. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1093. struct omap1_cam_dev *pcdev = ici->priv;
  1094. struct v4l2_pix_format *pix = &f->fmt.pix;
  1095. struct v4l2_mbus_framefmt mf;
  1096. int ret;
  1097. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1098. if (!xlate) {
  1099. dev_warn(dev, "%s: format %#x not found\n", __func__,
  1100. pix->pixelformat);
  1101. return -EINVAL;
  1102. }
  1103. mf.width = pix->width;
  1104. mf.height = pix->height;
  1105. mf.field = pix->field;
  1106. mf.colorspace = pix->colorspace;
  1107. mf.code = xlate->code;
  1108. ret = dma_align(&mf.width, &mf.height, xlate->host_fmt, pcdev->vb_mode,
  1109. true);
  1110. if (ret < 0) {
  1111. dev_err(dev, "%s: failed to align %ux%u %s with DMA\n",
  1112. __func__, pix->width, pix->height,
  1113. xlate->host_fmt->name);
  1114. return ret;
  1115. }
  1116. ret = set_mbus_format(pcdev, dev, icd, sd, &mf, xlate);
  1117. if (ret < 0) {
  1118. dev_err(dev, "%s: failed to set format\n", __func__);
  1119. return ret;
  1120. }
  1121. pix->width = mf.width;
  1122. pix->height = mf.height;
  1123. pix->field = mf.field;
  1124. pix->colorspace = mf.colorspace;
  1125. icd->current_fmt = xlate;
  1126. return 0;
  1127. }
  1128. static int omap1_cam_try_fmt(struct soc_camera_device *icd,
  1129. struct v4l2_format *f)
  1130. {
  1131. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1132. const struct soc_camera_format_xlate *xlate;
  1133. struct v4l2_pix_format *pix = &f->fmt.pix;
  1134. struct v4l2_mbus_framefmt mf;
  1135. int ret;
  1136. /* TODO: limit to mx1 hardware capabilities */
  1137. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  1138. if (!xlate) {
  1139. dev_warn(icd->parent, "Format %#x not found\n",
  1140. pix->pixelformat);
  1141. return -EINVAL;
  1142. }
  1143. mf.width = pix->width;
  1144. mf.height = pix->height;
  1145. mf.field = pix->field;
  1146. mf.colorspace = pix->colorspace;
  1147. mf.code = xlate->code;
  1148. /* limit to sensor capabilities */
  1149. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  1150. if (ret < 0)
  1151. return ret;
  1152. pix->width = mf.width;
  1153. pix->height = mf.height;
  1154. pix->field = mf.field;
  1155. pix->colorspace = mf.colorspace;
  1156. return 0;
  1157. }
  1158. static bool sg_mode;
  1159. /*
  1160. * Local mmap_mapper wrapper,
  1161. * used for detecting videobuf-dma-contig buffer allocation failures
  1162. * and switching to videobuf-dma-sg automatically for future attempts.
  1163. */
  1164. static int omap1_cam_mmap_mapper(struct videobuf_queue *q,
  1165. struct videobuf_buffer *buf,
  1166. struct vm_area_struct *vma)
  1167. {
  1168. struct soc_camera_device *icd = q->priv_data;
  1169. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1170. struct omap1_cam_dev *pcdev = ici->priv;
  1171. int ret;
  1172. ret = pcdev->mmap_mapper(q, buf, vma);
  1173. if (ret == -ENOMEM)
  1174. sg_mode = true;
  1175. return ret;
  1176. }
  1177. static void omap1_cam_init_videobuf(struct videobuf_queue *q,
  1178. struct soc_camera_device *icd)
  1179. {
  1180. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  1181. struct omap1_cam_dev *pcdev = ici->priv;
  1182. if (!sg_mode)
  1183. videobuf_queue_dma_contig_init(q, &omap1_videobuf_ops,
  1184. icd->parent, &pcdev->lock,
  1185. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  1186. sizeof(struct omap1_cam_buf), icd, &icd->video_lock);
  1187. else
  1188. videobuf_queue_sg_init(q, &omap1_videobuf_ops,
  1189. icd->parent, &pcdev->lock,
  1190. V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FIELD_NONE,
  1191. sizeof(struct omap1_cam_buf), icd, &icd->video_lock);
  1192. /* use videobuf mode (auto)selected with the module parameter */
  1193. pcdev->vb_mode = sg_mode ? OMAP1_CAM_DMA_SG : OMAP1_CAM_DMA_CONTIG;
  1194. /*
  1195. * Ensure we substitute the videobuf-dma-contig version of the
  1196. * mmap_mapper() callback with our own wrapper, used for switching
  1197. * automatically to videobuf-dma-sg on buffer allocation failure.
  1198. */
  1199. if (!sg_mode && q->int_ops->mmap_mapper != omap1_cam_mmap_mapper) {
  1200. pcdev->mmap_mapper = q->int_ops->mmap_mapper;
  1201. q->int_ops->mmap_mapper = omap1_cam_mmap_mapper;
  1202. }
  1203. }
  1204. static int omap1_cam_reqbufs(struct soc_camera_device *icd,
  1205. struct v4l2_requestbuffers *p)
  1206. {
  1207. int i;
  1208. /*
  1209. * This is for locking debugging only. I removed spinlocks and now I
  1210. * check whether .prepare is ever called on a linked buffer, or whether
  1211. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1212. * it hadn't triggered
  1213. */
  1214. for (i = 0; i < p->count; i++) {
  1215. struct omap1_cam_buf *buf = container_of(icd->vb_vidq.bufs[i],
  1216. struct omap1_cam_buf, vb);
  1217. buf->inwork = 0;
  1218. INIT_LIST_HEAD(&buf->vb.queue);
  1219. }
  1220. return 0;
  1221. }
  1222. static int omap1_cam_querycap(struct soc_camera_host *ici,
  1223. struct v4l2_capability *cap)
  1224. {
  1225. /* cap->name is set by the friendly caller:-> */
  1226. strlcpy(cap->card, "OMAP1 Camera", sizeof(cap->card));
  1227. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1228. return 0;
  1229. }
  1230. static int omap1_cam_set_bus_param(struct soc_camera_device *icd)
  1231. {
  1232. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1233. struct device *dev = icd->parent;
  1234. struct soc_camera_host *ici = to_soc_camera_host(dev);
  1235. struct omap1_cam_dev *pcdev = ici->priv;
  1236. u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
  1237. const struct soc_camera_format_xlate *xlate;
  1238. const struct soc_mbus_pixelfmt *fmt;
  1239. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  1240. unsigned long common_flags;
  1241. u32 ctrlclock, mode;
  1242. int ret;
  1243. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  1244. if (!ret) {
  1245. common_flags = soc_mbus_config_compatible(&cfg, SOCAM_BUS_FLAGS);
  1246. if (!common_flags) {
  1247. dev_warn(dev,
  1248. "Flags incompatible: camera 0x%x, host 0x%x\n",
  1249. cfg.flags, SOCAM_BUS_FLAGS);
  1250. return -EINVAL;
  1251. }
  1252. } else if (ret != -ENOIOCTLCMD) {
  1253. return ret;
  1254. } else {
  1255. common_flags = SOCAM_BUS_FLAGS;
  1256. }
  1257. /* Make choices, possibly based on platform configuration */
  1258. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  1259. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  1260. if (!pcdev->pdata ||
  1261. pcdev->pdata->flags & OMAP1_CAMERA_LCLK_RISING)
  1262. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  1263. else
  1264. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  1265. }
  1266. cfg.flags = common_flags;
  1267. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  1268. if (ret < 0 && ret != -ENOIOCTLCMD) {
  1269. dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
  1270. common_flags, ret);
  1271. return ret;
  1272. }
  1273. ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  1274. if (ctrlclock & LCLK_EN)
  1275. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  1276. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) {
  1277. dev_dbg(dev, "CTRLCLOCK_REG |= POLCLK\n");
  1278. ctrlclock |= POLCLK;
  1279. } else {
  1280. dev_dbg(dev, "CTRLCLOCK_REG &= ~POLCLK\n");
  1281. ctrlclock &= ~POLCLK;
  1282. }
  1283. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  1284. if (ctrlclock & LCLK_EN)
  1285. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  1286. /* select bus endianess */
  1287. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1288. fmt = xlate->host_fmt;
  1289. mode = CAM_READ(pcdev, MODE) & ~(RAZ_FIFO | IRQ_MASK | DMA);
  1290. if (fmt->order == SOC_MBUS_ORDER_LE) {
  1291. dev_dbg(dev, "MODE_REG &= ~ORDERCAMD\n");
  1292. CAM_WRITE(pcdev, MODE, mode & ~ORDERCAMD);
  1293. } else {
  1294. dev_dbg(dev, "MODE_REG |= ORDERCAMD\n");
  1295. CAM_WRITE(pcdev, MODE, mode | ORDERCAMD);
  1296. }
  1297. return 0;
  1298. }
  1299. static unsigned int omap1_cam_poll(struct file *file, poll_table *pt)
  1300. {
  1301. struct soc_camera_device *icd = file->private_data;
  1302. struct omap1_cam_buf *buf;
  1303. buf = list_entry(icd->vb_vidq.stream.next, struct omap1_cam_buf,
  1304. vb.stream);
  1305. poll_wait(file, &buf->vb.done, pt);
  1306. if (buf->vb.state == VIDEOBUF_DONE ||
  1307. buf->vb.state == VIDEOBUF_ERROR)
  1308. return POLLIN | POLLRDNORM;
  1309. return 0;
  1310. }
  1311. static struct soc_camera_host_ops omap1_host_ops = {
  1312. .owner = THIS_MODULE,
  1313. .add = omap1_cam_add_device,
  1314. .remove = omap1_cam_remove_device,
  1315. .get_formats = omap1_cam_get_formats,
  1316. .set_crop = omap1_cam_set_crop,
  1317. .set_fmt = omap1_cam_set_fmt,
  1318. .try_fmt = omap1_cam_try_fmt,
  1319. .init_videobuf = omap1_cam_init_videobuf,
  1320. .reqbufs = omap1_cam_reqbufs,
  1321. .querycap = omap1_cam_querycap,
  1322. .set_bus_param = omap1_cam_set_bus_param,
  1323. .poll = omap1_cam_poll,
  1324. };
  1325. static int __init omap1_cam_probe(struct platform_device *pdev)
  1326. {
  1327. struct omap1_cam_dev *pcdev;
  1328. struct resource *res;
  1329. struct clk *clk;
  1330. void __iomem *base;
  1331. unsigned int irq;
  1332. int err = 0;
  1333. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1334. irq = platform_get_irq(pdev, 0);
  1335. if (!res || (int)irq <= 0) {
  1336. err = -ENODEV;
  1337. goto exit;
  1338. }
  1339. clk = clk_get(&pdev->dev, "armper_ck");
  1340. if (IS_ERR(clk)) {
  1341. err = PTR_ERR(clk);
  1342. goto exit;
  1343. }
  1344. pcdev = kzalloc(sizeof(*pcdev) + resource_size(res), GFP_KERNEL);
  1345. if (!pcdev) {
  1346. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1347. err = -ENOMEM;
  1348. goto exit_put_clk;
  1349. }
  1350. pcdev->res = res;
  1351. pcdev->clk = clk;
  1352. pcdev->pdata = pdev->dev.platform_data;
  1353. if (pcdev->pdata) {
  1354. pcdev->pflags = pcdev->pdata->flags;
  1355. pcdev->camexclk = pcdev->pdata->camexclk_khz * 1000;
  1356. }
  1357. switch (pcdev->camexclk) {
  1358. case 6000000:
  1359. case 8000000:
  1360. case 9600000:
  1361. case 12000000:
  1362. case 24000000:
  1363. break;
  1364. default:
  1365. /* pcdev->camexclk != 0 => pcdev->pdata != NULL */
  1366. dev_warn(&pdev->dev,
  1367. "Incorrect sensor clock frequency %ld kHz, "
  1368. "should be one of 0, 6, 8, 9.6, 12 or 24 MHz, "
  1369. "please correct your platform data\n",
  1370. pcdev->pdata->camexclk_khz);
  1371. pcdev->camexclk = 0;
  1372. case 0:
  1373. dev_info(&pdev->dev, "Not providing sensor clock\n");
  1374. }
  1375. INIT_LIST_HEAD(&pcdev->capture);
  1376. spin_lock_init(&pcdev->lock);
  1377. /*
  1378. * Request the region.
  1379. */
  1380. if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
  1381. err = -EBUSY;
  1382. goto exit_kfree;
  1383. }
  1384. base = ioremap(res->start, resource_size(res));
  1385. if (!base) {
  1386. err = -ENOMEM;
  1387. goto exit_release;
  1388. }
  1389. pcdev->irq = irq;
  1390. pcdev->base = base;
  1391. sensor_reset(pcdev, true);
  1392. err = omap_request_dma(OMAP_DMA_CAMERA_IF_RX, DRIVER_NAME,
  1393. dma_isr, (void *)pcdev, &pcdev->dma_ch);
  1394. if (err < 0) {
  1395. dev_err(&pdev->dev, "Can't request DMA for OMAP1 Camera\n");
  1396. err = -EBUSY;
  1397. goto exit_iounmap;
  1398. }
  1399. dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_ch);
  1400. /* preconfigure DMA */
  1401. omap_set_dma_src_params(pcdev->dma_ch, OMAP_DMA_PORT_TIPB,
  1402. OMAP_DMA_AMODE_CONSTANT, res->start + REG_CAMDATA,
  1403. 0, 0);
  1404. omap_set_dma_dest_burst_mode(pcdev->dma_ch, OMAP_DMA_DATA_BURST_4);
  1405. /* setup DMA autoinitialization */
  1406. omap_dma_link_lch(pcdev->dma_ch, pcdev->dma_ch);
  1407. err = request_irq(pcdev->irq, cam_isr, 0, DRIVER_NAME, pcdev);
  1408. if (err) {
  1409. dev_err(&pdev->dev, "Camera interrupt register failed\n");
  1410. goto exit_free_dma;
  1411. }
  1412. pcdev->soc_host.drv_name = DRIVER_NAME;
  1413. pcdev->soc_host.ops = &omap1_host_ops;
  1414. pcdev->soc_host.priv = pcdev;
  1415. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  1416. pcdev->soc_host.nr = pdev->id;
  1417. err = soc_camera_host_register(&pcdev->soc_host);
  1418. if (err)
  1419. goto exit_free_irq;
  1420. dev_info(&pdev->dev, "OMAP1 Camera Interface driver loaded\n");
  1421. return 0;
  1422. exit_free_irq:
  1423. free_irq(pcdev->irq, pcdev);
  1424. exit_free_dma:
  1425. omap_free_dma(pcdev->dma_ch);
  1426. exit_iounmap:
  1427. iounmap(base);
  1428. exit_release:
  1429. release_mem_region(res->start, resource_size(res));
  1430. exit_kfree:
  1431. kfree(pcdev);
  1432. exit_put_clk:
  1433. clk_put(clk);
  1434. exit:
  1435. return err;
  1436. }
  1437. static int __exit omap1_cam_remove(struct platform_device *pdev)
  1438. {
  1439. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1440. struct omap1_cam_dev *pcdev = container_of(soc_host,
  1441. struct omap1_cam_dev, soc_host);
  1442. struct resource *res;
  1443. free_irq(pcdev->irq, pcdev);
  1444. omap_free_dma(pcdev->dma_ch);
  1445. soc_camera_host_unregister(soc_host);
  1446. iounmap(pcdev->base);
  1447. res = pcdev->res;
  1448. release_mem_region(res->start, resource_size(res));
  1449. clk_put(pcdev->clk);
  1450. kfree(pcdev);
  1451. dev_info(&pdev->dev, "OMAP1 Camera Interface driver unloaded\n");
  1452. return 0;
  1453. }
  1454. static struct platform_driver omap1_cam_driver = {
  1455. .driver = {
  1456. .name = DRIVER_NAME,
  1457. },
  1458. .probe = omap1_cam_probe,
  1459. .remove = __exit_p(omap1_cam_remove),
  1460. };
  1461. module_platform_driver(omap1_cam_driver);
  1462. module_param(sg_mode, bool, 0644);
  1463. MODULE_PARM_DESC(sg_mode, "videobuf mode, 0: dma-contig (default), 1: dma-sg");
  1464. MODULE_DESCRIPTION("OMAP1 Camera Interface driver");
  1465. MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
  1466. MODULE_LICENSE("GPL v2");
  1467. MODULE_VERSION(DRIVER_VERSION);
  1468. MODULE_ALIAS("platform:" DRIVER_NAME);