omap1_camera.c 45 KB

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