omap1_camera.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  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. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  167. icd->current_fmt->host_fmt);
  168. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  169. struct omap1_cam_dev *pcdev = ici->priv;
  170. if (bytes_per_line < 0)
  171. return bytes_per_line;
  172. *size = bytes_per_line * icd->user_height;
  173. if (!*count || *count < OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode))
  174. *count = OMAP1_CAMERA_MIN_BUF_COUNT(pcdev->vb_mode);
  175. if (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
  176. *count = (MAX_VIDEO_MEM * 1024 * 1024) / *size;
  177. dev_dbg(icd->parent,
  178. "%s: count=%d, size=%d\n", __func__, *count, *size);
  179. return 0;
  180. }
  181. static void free_buffer(struct videobuf_queue *vq, struct omap1_cam_buf *buf,
  182. enum omap1_cam_vb_mode vb_mode)
  183. {
  184. struct videobuf_buffer *vb = &buf->vb;
  185. BUG_ON(in_interrupt());
  186. videobuf_waiton(vq, vb, 0, 0);
  187. if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
  188. videobuf_dma_contig_free(vq, vb);
  189. } else {
  190. struct soc_camera_device *icd = vq->priv_data;
  191. struct device *dev = icd->parent;
  192. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  193. videobuf_dma_unmap(dev, dma);
  194. videobuf_dma_free(dma);
  195. }
  196. vb->state = VIDEOBUF_NEEDS_INIT;
  197. }
  198. static int omap1_videobuf_prepare(struct videobuf_queue *vq,
  199. struct videobuf_buffer *vb, enum v4l2_field field)
  200. {
  201. struct soc_camera_device *icd = vq->priv_data;
  202. struct omap1_cam_buf *buf = container_of(vb, struct omap1_cam_buf, vb);
  203. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  204. icd->current_fmt->host_fmt);
  205. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  206. struct omap1_cam_dev *pcdev = ici->priv;
  207. int ret;
  208. if (bytes_per_line < 0)
  209. return bytes_per_line;
  210. WARN_ON(!list_empty(&vb->queue));
  211. BUG_ON(NULL == icd->current_fmt);
  212. buf->inwork = 1;
  213. if (buf->code != icd->current_fmt->code || vb->field != field ||
  214. vb->width != icd->user_width ||
  215. vb->height != icd->user_height) {
  216. buf->code = icd->current_fmt->code;
  217. vb->width = icd->user_width;
  218. vb->height = icd->user_height;
  219. vb->field = field;
  220. vb->state = VIDEOBUF_NEEDS_INIT;
  221. }
  222. vb->size = bytes_per_line * vb->height;
  223. if (vb->baddr && vb->bsize < vb->size) {
  224. ret = -EINVAL;
  225. goto out;
  226. }
  227. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  228. ret = videobuf_iolock(vq, vb, NULL);
  229. if (ret)
  230. goto fail;
  231. vb->state = VIDEOBUF_PREPARED;
  232. }
  233. buf->inwork = 0;
  234. return 0;
  235. fail:
  236. free_buffer(vq, buf, pcdev->vb_mode);
  237. out:
  238. buf->inwork = 0;
  239. return ret;
  240. }
  241. static void set_dma_dest_params(int dma_ch, struct omap1_cam_buf *buf,
  242. enum omap1_cam_vb_mode vb_mode)
  243. {
  244. dma_addr_t dma_addr;
  245. unsigned int block_size;
  246. if (vb_mode == OMAP1_CAM_DMA_CONTIG) {
  247. dma_addr = videobuf_to_dma_contig(&buf->vb);
  248. block_size = buf->vb.size;
  249. } else {
  250. if (WARN_ON(!buf->sgbuf)) {
  251. buf->result = VIDEOBUF_ERROR;
  252. return;
  253. }
  254. dma_addr = sg_dma_address(buf->sgbuf);
  255. if (WARN_ON(!dma_addr)) {
  256. buf->sgbuf = NULL;
  257. buf->result = VIDEOBUF_ERROR;
  258. return;
  259. }
  260. block_size = sg_dma_len(buf->sgbuf);
  261. if (WARN_ON(!block_size)) {
  262. buf->sgbuf = NULL;
  263. buf->result = VIDEOBUF_ERROR;
  264. return;
  265. }
  266. if (unlikely(buf->bytes_left < block_size))
  267. block_size = buf->bytes_left;
  268. if (WARN_ON(dma_addr & (DMA_FRAME_SIZE(vb_mode) *
  269. DMA_ELEMENT_SIZE - 1))) {
  270. dma_addr = ALIGN(dma_addr, DMA_FRAME_SIZE(vb_mode) *
  271. DMA_ELEMENT_SIZE);
  272. block_size &= ~(DMA_FRAME_SIZE(vb_mode) *
  273. DMA_ELEMENT_SIZE - 1);
  274. }
  275. buf->bytes_left -= block_size;
  276. buf->sgcount++;
  277. }
  278. omap_set_dma_dest_params(dma_ch,
  279. OMAP_DMA_PORT_EMIFF, OMAP_DMA_AMODE_POST_INC, dma_addr, 0, 0);
  280. omap_set_dma_transfer_params(dma_ch,
  281. OMAP_DMA_DATA_TYPE_S32, DMA_FRAME_SIZE(vb_mode),
  282. block_size >> (DMA_FRAME_SHIFT(vb_mode) + DMA_ELEMENT_SHIFT),
  283. DMA_SYNC, 0, 0);
  284. }
  285. static struct omap1_cam_buf *prepare_next_vb(struct omap1_cam_dev *pcdev)
  286. {
  287. struct omap1_cam_buf *buf;
  288. /*
  289. * If there is already a buffer pointed out by the pcdev->ready,
  290. * (re)use it, otherwise try to fetch and configure a new one.
  291. */
  292. buf = pcdev->ready;
  293. if (!buf) {
  294. if (list_empty(&pcdev->capture))
  295. return buf;
  296. buf = list_entry(pcdev->capture.next,
  297. struct omap1_cam_buf, vb.queue);
  298. buf->vb.state = VIDEOBUF_ACTIVE;
  299. pcdev->ready = buf;
  300. list_del_init(&buf->vb.queue);
  301. }
  302. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  303. /*
  304. * In CONTIG mode, we can safely enter next buffer parameters
  305. * into the DMA programming register set after the DMA
  306. * has already been activated on the previous buffer
  307. */
  308. set_dma_dest_params(pcdev->dma_ch, buf, pcdev->vb_mode);
  309. } else {
  310. /*
  311. * In SG mode, the above is not safe since there are probably
  312. * a bunch of sgbufs from previous sglist still pending.
  313. * Instead, mark the sglist fresh for the upcoming
  314. * try_next_sgbuf().
  315. */
  316. buf->sgbuf = NULL;
  317. }
  318. return buf;
  319. }
  320. static struct scatterlist *try_next_sgbuf(int dma_ch, struct omap1_cam_buf *buf)
  321. {
  322. struct scatterlist *sgbuf;
  323. if (likely(buf->sgbuf)) {
  324. /* current sglist is active */
  325. if (unlikely(!buf->bytes_left)) {
  326. /* indicate sglist complete */
  327. sgbuf = NULL;
  328. } else {
  329. /* process next sgbuf */
  330. sgbuf = sg_next(buf->sgbuf);
  331. if (WARN_ON(!sgbuf)) {
  332. buf->result = VIDEOBUF_ERROR;
  333. } else if (WARN_ON(!sg_dma_len(sgbuf))) {
  334. sgbuf = NULL;
  335. buf->result = VIDEOBUF_ERROR;
  336. }
  337. }
  338. buf->sgbuf = sgbuf;
  339. } else {
  340. /* sglist is fresh, initialize it before using */
  341. struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
  342. sgbuf = dma->sglist;
  343. if (!(WARN_ON(!sgbuf))) {
  344. buf->sgbuf = sgbuf;
  345. buf->sgcount = 0;
  346. buf->bytes_left = buf->vb.size;
  347. buf->result = VIDEOBUF_DONE;
  348. }
  349. }
  350. if (sgbuf)
  351. /*
  352. * Put our next sgbuf parameters (address, size)
  353. * into the DMA programming register set.
  354. */
  355. set_dma_dest_params(dma_ch, buf, OMAP1_CAM_DMA_SG);
  356. return sgbuf;
  357. }
  358. static void start_capture(struct omap1_cam_dev *pcdev)
  359. {
  360. struct omap1_cam_buf *buf = pcdev->active;
  361. u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  362. u32 mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN;
  363. if (WARN_ON(!buf))
  364. return;
  365. /*
  366. * Enable start of frame interrupt, which we will use for activating
  367. * our end of frame watchdog when capture actually starts.
  368. */
  369. mode |= EN_V_UP;
  370. if (unlikely(ctrlclock & LCLK_EN))
  371. /* stop pixel clock before FIFO reset */
  372. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  373. /* reset FIFO */
  374. CAM_WRITE(pcdev, MODE, mode | RAZ_FIFO);
  375. omap_start_dma(pcdev->dma_ch);
  376. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  377. /*
  378. * In SG mode, it's a good moment for fetching next sgbuf
  379. * from the current sglist and, if available, already putting
  380. * its parameters into the DMA programming register set.
  381. */
  382. try_next_sgbuf(pcdev->dma_ch, buf);
  383. }
  384. /* (re)enable pixel clock */
  385. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | LCLK_EN);
  386. /* release FIFO reset */
  387. CAM_WRITE(pcdev, MODE, mode);
  388. }
  389. static void suspend_capture(struct omap1_cam_dev *pcdev)
  390. {
  391. u32 ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  392. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~LCLK_EN);
  393. omap_stop_dma(pcdev->dma_ch);
  394. }
  395. static void disable_capture(struct omap1_cam_dev *pcdev)
  396. {
  397. u32 mode = CAM_READ_CACHE(pcdev, MODE);
  398. CAM_WRITE(pcdev, MODE, mode & ~(IRQ_MASK | DMA));
  399. }
  400. static void omap1_videobuf_queue(struct videobuf_queue *vq,
  401. struct videobuf_buffer *vb)
  402. {
  403. struct soc_camera_device *icd = vq->priv_data;
  404. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  405. struct omap1_cam_dev *pcdev = ici->priv;
  406. struct omap1_cam_buf *buf;
  407. u32 mode;
  408. list_add_tail(&vb->queue, &pcdev->capture);
  409. vb->state = VIDEOBUF_QUEUED;
  410. if (pcdev->active) {
  411. /*
  412. * Capture in progress, so don't touch pcdev->ready even if
  413. * empty. Since the transfer of the DMA programming register set
  414. * content to the DMA working register set is done automatically
  415. * by the DMA hardware, this can pretty well happen while we
  416. * are keeping the lock here. Leave fetching it from the queue
  417. * to be done when a next DMA interrupt occures instead.
  418. */
  419. return;
  420. }
  421. WARN_ON(pcdev->ready);
  422. buf = prepare_next_vb(pcdev);
  423. if (WARN_ON(!buf))
  424. return;
  425. pcdev->active = buf;
  426. pcdev->ready = NULL;
  427. dev_dbg(icd->parent,
  428. "%s: capture not active, setup FIFO, start DMA\n", __func__);
  429. mode = CAM_READ_CACHE(pcdev, MODE) & ~THRESHOLD_MASK;
  430. mode |= THRESHOLD_LEVEL(pcdev->vb_mode) << THRESHOLD_SHIFT;
  431. CAM_WRITE(pcdev, MODE, mode | EN_FIFO_FULL | DMA);
  432. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  433. /*
  434. * In SG mode, the above prepare_next_vb() didn't actually
  435. * put anything into the DMA programming register set,
  436. * so we have to do it now, before activating DMA.
  437. */
  438. try_next_sgbuf(pcdev->dma_ch, buf);
  439. }
  440. start_capture(pcdev);
  441. }
  442. static void omap1_videobuf_release(struct videobuf_queue *vq,
  443. struct videobuf_buffer *vb)
  444. {
  445. struct omap1_cam_buf *buf =
  446. container_of(vb, struct omap1_cam_buf, vb);
  447. struct soc_camera_device *icd = vq->priv_data;
  448. struct device *dev = icd->parent;
  449. struct soc_camera_host *ici = to_soc_camera_host(dev);
  450. struct omap1_cam_dev *pcdev = ici->priv;
  451. switch (vb->state) {
  452. case VIDEOBUF_DONE:
  453. dev_dbg(dev, "%s (done)\n", __func__);
  454. break;
  455. case VIDEOBUF_ACTIVE:
  456. dev_dbg(dev, "%s (active)\n", __func__);
  457. break;
  458. case VIDEOBUF_QUEUED:
  459. dev_dbg(dev, "%s (queued)\n", __func__);
  460. break;
  461. case VIDEOBUF_PREPARED:
  462. dev_dbg(dev, "%s (prepared)\n", __func__);
  463. break;
  464. default:
  465. dev_dbg(dev, "%s (unknown %d)\n", __func__, vb->state);
  466. break;
  467. }
  468. free_buffer(vq, buf, pcdev->vb_mode);
  469. }
  470. static void videobuf_done(struct omap1_cam_dev *pcdev,
  471. enum videobuf_state result)
  472. {
  473. struct omap1_cam_buf *buf = pcdev->active;
  474. struct videobuf_buffer *vb;
  475. struct device *dev = pcdev->icd->parent;
  476. if (WARN_ON(!buf)) {
  477. suspend_capture(pcdev);
  478. disable_capture(pcdev);
  479. return;
  480. }
  481. if (result == VIDEOBUF_ERROR)
  482. suspend_capture(pcdev);
  483. vb = &buf->vb;
  484. if (waitqueue_active(&vb->done)) {
  485. if (!pcdev->ready && result != VIDEOBUF_ERROR) {
  486. /*
  487. * No next buffer has been entered into the DMA
  488. * programming register set on time (could be done only
  489. * while the previous DMA interurpt was processed, not
  490. * later), so the last DMA block, be it a whole buffer
  491. * if in CONTIG or its last sgbuf if in SG mode, is
  492. * about to be reused by the just autoreinitialized DMA
  493. * engine, and overwritten with next frame data. Best we
  494. * can do is stopping the capture as soon as possible,
  495. * hopefully before the next frame start.
  496. */
  497. suspend_capture(pcdev);
  498. }
  499. vb->state = result;
  500. do_gettimeofday(&vb->ts);
  501. if (result != VIDEOBUF_ERROR)
  502. vb->field_count++;
  503. wake_up(&vb->done);
  504. /* shift in next buffer */
  505. buf = pcdev->ready;
  506. pcdev->active = buf;
  507. pcdev->ready = NULL;
  508. if (!buf) {
  509. /*
  510. * No next buffer was ready on time (see above), so
  511. * indicate error condition to force capture restart or
  512. * stop, depending on next buffer already queued or not.
  513. */
  514. result = VIDEOBUF_ERROR;
  515. prepare_next_vb(pcdev);
  516. buf = pcdev->ready;
  517. pcdev->active = buf;
  518. pcdev->ready = NULL;
  519. }
  520. } else if (pcdev->ready) {
  521. /*
  522. * In both CONTIG and SG mode, the DMA engine has possibly
  523. * been already autoreinitialized with the preprogrammed
  524. * pcdev->ready buffer. We can either accept this fact
  525. * and just swap the buffers, or provoke an error condition
  526. * and restart capture. The former seems less intrusive.
  527. */
  528. dev_dbg(dev, "%s: nobody waiting on videobuf, swap with next\n",
  529. __func__);
  530. pcdev->active = pcdev->ready;
  531. if (pcdev->vb_mode == OMAP1_CAM_DMA_SG) {
  532. /*
  533. * In SG mode, we have to make sure that the buffer we
  534. * are putting back into the pcdev->ready is marked
  535. * fresh.
  536. */
  537. buf->sgbuf = NULL;
  538. }
  539. pcdev->ready = buf;
  540. buf = pcdev->active;
  541. } else {
  542. /*
  543. * No next buffer has been entered into
  544. * the DMA programming register set on time.
  545. */
  546. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  547. /*
  548. * In CONTIG mode, the DMA engine has already been
  549. * reinitialized with the current buffer. Best we can do
  550. * is not touching it.
  551. */
  552. dev_dbg(dev,
  553. "%s: nobody waiting on videobuf, reuse it\n",
  554. __func__);
  555. } else {
  556. /*
  557. * In SG mode, the DMA engine has just been
  558. * autoreinitialized with the last sgbuf from the
  559. * current list. Restart capture in order to transfer
  560. * next frame start into the first sgbuf, not the last
  561. * one.
  562. */
  563. if (result != VIDEOBUF_ERROR) {
  564. suspend_capture(pcdev);
  565. result = VIDEOBUF_ERROR;
  566. }
  567. }
  568. }
  569. if (!buf) {
  570. dev_dbg(dev, "%s: no more videobufs, stop capture\n", __func__);
  571. disable_capture(pcdev);
  572. return;
  573. }
  574. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  575. /*
  576. * In CONTIG mode, the current buffer parameters had already
  577. * been entered into the DMA programming register set while the
  578. * buffer was fetched with prepare_next_vb(), they may have also
  579. * been transferred into the runtime set and already active if
  580. * the DMA still running.
  581. */
  582. } else {
  583. /* In SG mode, extra steps are required */
  584. if (result == VIDEOBUF_ERROR)
  585. /* make sure we (re)use sglist from start on error */
  586. buf->sgbuf = NULL;
  587. /*
  588. * In any case, enter the next sgbuf parameters into the DMA
  589. * programming register set. They will be used either during
  590. * nearest DMA autoreinitialization or, in case of an error,
  591. * on DMA startup below.
  592. */
  593. try_next_sgbuf(pcdev->dma_ch, buf);
  594. }
  595. if (result == VIDEOBUF_ERROR) {
  596. dev_dbg(dev, "%s: videobuf error; reset FIFO, restart DMA\n",
  597. __func__);
  598. start_capture(pcdev);
  599. /*
  600. * In SG mode, the above also resulted in the next sgbuf
  601. * parameters being entered into the DMA programming register
  602. * set, making them ready for next DMA autoreinitialization.
  603. */
  604. }
  605. /*
  606. * Finally, try fetching next buffer.
  607. * In CONTIG mode, it will also enter it into the DMA programming
  608. * register set, making it ready for next DMA autoreinitialization.
  609. */
  610. prepare_next_vb(pcdev);
  611. }
  612. static void dma_isr(int channel, unsigned short status, void *data)
  613. {
  614. struct omap1_cam_dev *pcdev = data;
  615. struct omap1_cam_buf *buf = pcdev->active;
  616. unsigned long flags;
  617. spin_lock_irqsave(&pcdev->lock, flags);
  618. if (WARN_ON(!buf)) {
  619. suspend_capture(pcdev);
  620. disable_capture(pcdev);
  621. goto out;
  622. }
  623. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  624. /*
  625. * In CONTIG mode, assume we have just managed to collect the
  626. * whole frame, hopefully before our end of frame watchdog is
  627. * triggered. Then, all we have to do is disabling the watchdog
  628. * for this frame, and calling videobuf_done() with success
  629. * indicated.
  630. */
  631. CAM_WRITE(pcdev, MODE,
  632. CAM_READ_CACHE(pcdev, MODE) & ~EN_V_DOWN);
  633. videobuf_done(pcdev, VIDEOBUF_DONE);
  634. } else {
  635. /*
  636. * In SG mode, we have to process every sgbuf from the current
  637. * sglist, one after another.
  638. */
  639. if (buf->sgbuf) {
  640. /*
  641. * Current sglist not completed yet, try fetching next
  642. * sgbuf, hopefully putting it into the DMA programming
  643. * register set, making it ready for next DMA
  644. * autoreinitialization.
  645. */
  646. try_next_sgbuf(pcdev->dma_ch, buf);
  647. if (buf->sgbuf)
  648. goto out;
  649. /*
  650. * No more sgbufs left in the current sglist. This
  651. * doesn't mean that the whole videobuffer is already
  652. * complete, but only that the last sgbuf from the
  653. * current sglist is about to be filled. It will be
  654. * ready on next DMA interrupt, signalled with the
  655. * buf->sgbuf set back to NULL.
  656. */
  657. if (buf->result != VIDEOBUF_ERROR) {
  658. /*
  659. * Video frame collected without errors so far,
  660. * we can prepare for collecting a next one
  661. * as soon as DMA gets autoreinitialized
  662. * after the current (last) sgbuf is completed.
  663. */
  664. buf = prepare_next_vb(pcdev);
  665. if (!buf)
  666. goto out;
  667. try_next_sgbuf(pcdev->dma_ch, buf);
  668. goto out;
  669. }
  670. }
  671. /* end of videobuf */
  672. videobuf_done(pcdev, buf->result);
  673. }
  674. out:
  675. spin_unlock_irqrestore(&pcdev->lock, flags);
  676. }
  677. static irqreturn_t cam_isr(int irq, void *data)
  678. {
  679. struct omap1_cam_dev *pcdev = data;
  680. struct device *dev = pcdev->icd->parent;
  681. struct omap1_cam_buf *buf = pcdev->active;
  682. u32 it_status;
  683. unsigned long flags;
  684. it_status = CAM_READ(pcdev, IT_STATUS);
  685. if (!it_status)
  686. return IRQ_NONE;
  687. spin_lock_irqsave(&pcdev->lock, flags);
  688. if (WARN_ON(!buf)) {
  689. dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
  690. __func__, it_status);
  691. suspend_capture(pcdev);
  692. disable_capture(pcdev);
  693. goto out;
  694. }
  695. if (unlikely(it_status & FIFO_FULL)) {
  696. dev_warn(dev, "%s: FIFO overflow\n", __func__);
  697. } else if (it_status & V_DOWN) {
  698. /* end of video frame watchdog */
  699. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  700. /*
  701. * In CONTIG mode, the watchdog is disabled with
  702. * successful DMA end of block interrupt, and reenabled
  703. * on next frame start. If we get here, there is nothing
  704. * to check, we must be out of sync.
  705. */
  706. } else {
  707. if (buf->sgcount == 2) {
  708. /*
  709. * If exactly 2 sgbufs from the next sglist have
  710. * been programmed into the DMA engine (the
  711. * first one already transferred into the DMA
  712. * runtime register set, the second one still
  713. * in the programming set), then we are in sync.
  714. */
  715. goto out;
  716. }
  717. }
  718. dev_notice(dev, "%s: unexpected end of video frame\n",
  719. __func__);
  720. } else if (it_status & V_UP) {
  721. u32 mode;
  722. if (pcdev->vb_mode == OMAP1_CAM_DMA_CONTIG) {
  723. /*
  724. * In CONTIG mode, we need this interrupt every frame
  725. * in oredr to reenable our end of frame watchdog.
  726. */
  727. mode = CAM_READ_CACHE(pcdev, MODE);
  728. } else {
  729. /*
  730. * In SG mode, the below enabled end of frame watchdog
  731. * is kept on permanently, so we can turn this one shot
  732. * setup off.
  733. */
  734. mode = CAM_READ_CACHE(pcdev, MODE) & ~EN_V_UP;
  735. }
  736. if (!(mode & EN_V_DOWN)) {
  737. /* (re)enable end of frame watchdog interrupt */
  738. mode |= EN_V_DOWN;
  739. }
  740. CAM_WRITE(pcdev, MODE, mode);
  741. goto out;
  742. } else {
  743. dev_warn(dev, "%s: unhandled camera interrupt, status == %#x\n",
  744. __func__, it_status);
  745. goto out;
  746. }
  747. videobuf_done(pcdev, VIDEOBUF_ERROR);
  748. out:
  749. spin_unlock_irqrestore(&pcdev->lock, flags);
  750. return IRQ_HANDLED;
  751. }
  752. static struct videobuf_queue_ops omap1_videobuf_ops = {
  753. .buf_setup = omap1_videobuf_setup,
  754. .buf_prepare = omap1_videobuf_prepare,
  755. .buf_queue = omap1_videobuf_queue,
  756. .buf_release = omap1_videobuf_release,
  757. };
  758. /*
  759. * SOC Camera host operations
  760. */
  761. static void sensor_reset(struct omap1_cam_dev *pcdev, bool reset)
  762. {
  763. /* apply/release camera sensor reset if requested by platform data */
  764. if (pcdev->pflags & OMAP1_CAMERA_RST_HIGH)
  765. CAM_WRITE(pcdev, GPIO, reset);
  766. else if (pcdev->pflags & OMAP1_CAMERA_RST_LOW)
  767. CAM_WRITE(pcdev, GPIO, !reset);
  768. }
  769. /*
  770. * The following two functions absolutely depend on the fact, that
  771. * there can be only one camera on OMAP1 camera sensor interface
  772. */
  773. static int omap1_cam_add_device(struct soc_camera_device *icd)
  774. {
  775. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  776. struct omap1_cam_dev *pcdev = ici->priv;
  777. u32 ctrlclock;
  778. if (pcdev->icd)
  779. return -EBUSY;
  780. clk_enable(pcdev->clk);
  781. /* setup sensor clock */
  782. ctrlclock = CAM_READ(pcdev, CTRLCLOCK);
  783. ctrlclock &= ~(CAMEXCLK_EN | MCLK_EN | DPLL_EN);
  784. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  785. ctrlclock &= ~FOSCMOD_MASK;
  786. switch (pcdev->camexclk) {
  787. case 6000000:
  788. ctrlclock |= CAMEXCLK_EN | FOSCMOD_6MHz;
  789. break;
  790. case 8000000:
  791. ctrlclock |= CAMEXCLK_EN | FOSCMOD_8MHz | DPLL_EN;
  792. break;
  793. case 9600000:
  794. ctrlclock |= CAMEXCLK_EN | FOSCMOD_9_6MHz | DPLL_EN;
  795. break;
  796. case 12000000:
  797. ctrlclock |= CAMEXCLK_EN | FOSCMOD_12MHz;
  798. break;
  799. case 24000000:
  800. ctrlclock |= CAMEXCLK_EN | FOSCMOD_24MHz | DPLL_EN;
  801. default:
  802. break;
  803. }
  804. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~DPLL_EN);
  805. /* enable internal clock */
  806. ctrlclock |= MCLK_EN;
  807. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  808. sensor_reset(pcdev, false);
  809. pcdev->icd = icd;
  810. dev_dbg(icd->parent, "OMAP1 Camera driver attached to camera %d\n",
  811. icd->devnum);
  812. return 0;
  813. }
  814. static void omap1_cam_remove_device(struct soc_camera_device *icd)
  815. {
  816. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  817. struct omap1_cam_dev *pcdev = ici->priv;
  818. u32 ctrlclock;
  819. BUG_ON(icd != pcdev->icd);
  820. suspend_capture(pcdev);
  821. disable_capture(pcdev);
  822. sensor_reset(pcdev, true);
  823. /* disable and release system clocks */
  824. ctrlclock = CAM_READ_CACHE(pcdev, CTRLCLOCK);
  825. ctrlclock &= ~(MCLK_EN | DPLL_EN | CAMEXCLK_EN);
  826. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  827. ctrlclock = (ctrlclock & ~FOSCMOD_MASK) | FOSCMOD_12MHz;
  828. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock);
  829. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock | MCLK_EN);
  830. CAM_WRITE(pcdev, CTRLCLOCK, ctrlclock & ~MCLK_EN);
  831. clk_disable(pcdev->clk);
  832. pcdev->icd = NULL;
  833. dev_dbg(icd->parent,
  834. "OMAP1 Camera driver detached from camera %d\n", icd->devnum);
  835. }
  836. /* Duplicate standard formats based on host capability of byte swapping */
  837. static const struct soc_mbus_lookup omap1_cam_formats[] = {
  838. {
  839. .code = V4L2_MBUS_FMT_UYVY8_2X8,
  840. .fmt = {
  841. .fourcc = V4L2_PIX_FMT_YUYV,
  842. .name = "YUYV",
  843. .bits_per_sample = 8,
  844. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  845. .order = SOC_MBUS_ORDER_BE,
  846. },
  847. }, {
  848. .code = V4L2_MBUS_FMT_VYUY8_2X8,
  849. .fmt = {
  850. .fourcc = V4L2_PIX_FMT_YVYU,
  851. .name = "YVYU",
  852. .bits_per_sample = 8,
  853. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  854. .order = SOC_MBUS_ORDER_BE,
  855. },
  856. }, {
  857. .code = V4L2_MBUS_FMT_YUYV8_2X8,
  858. .fmt = {
  859. .fourcc = V4L2_PIX_FMT_UYVY,
  860. .name = "UYVY",
  861. .bits_per_sample = 8,
  862. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  863. .order = SOC_MBUS_ORDER_BE,
  864. },
  865. }, {
  866. .code = V4L2_MBUS_FMT_YVYU8_2X8,
  867. .fmt = {
  868. .fourcc = V4L2_PIX_FMT_VYUY,
  869. .name = "VYUY",
  870. .bits_per_sample = 8,
  871. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  872. .order = SOC_MBUS_ORDER_BE,
  873. },
  874. }, {
  875. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_BE,
  876. .fmt = {
  877. .fourcc = V4L2_PIX_FMT_RGB555,
  878. .name = "RGB555",
  879. .bits_per_sample = 8,
  880. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  881. .order = SOC_MBUS_ORDER_BE,
  882. },
  883. }, {
  884. .code = V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  885. .fmt = {
  886. .fourcc = V4L2_PIX_FMT_RGB555X,
  887. .name = "RGB555X",
  888. .bits_per_sample = 8,
  889. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  890. .order = SOC_MBUS_ORDER_BE,
  891. },
  892. }, {
  893. .code = V4L2_MBUS_FMT_RGB565_2X8_BE,
  894. .fmt = {
  895. .fourcc = V4L2_PIX_FMT_RGB565,
  896. .name = "RGB565",
  897. .bits_per_sample = 8,
  898. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  899. .order = SOC_MBUS_ORDER_BE,
  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. },
  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);