mx1_camera.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * V4L2 Driver for i.MXL/i.MXL camera (CSI) host
  3. *
  4. * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
  5. * Copyright (C) 2009, Darius Augulis <augulis.darius@gmail.com>
  6. *
  7. * Based on PXA SoC camera driver
  8. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/mutex.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/sched.h>
  31. #include <linux/time.h>
  32. #include <linux/version.h>
  33. #include <linux/videodev2.h>
  34. #include <media/soc_camera.h>
  35. #include <media/v4l2-common.h>
  36. #include <media/v4l2-dev.h>
  37. #include <media/videobuf-dma-contig.h>
  38. #include <asm/dma.h>
  39. #include <asm/fiq.h>
  40. #include <mach/dma-mx1-mx2.h>
  41. #include <mach/hardware.h>
  42. #include <mach/mx1_camera.h>
  43. /*
  44. * CSI registers
  45. */
  46. #define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
  47. #define DMA_DIMR 0x08 /* Interrupt mask Register */
  48. #define CSICR1 0x00 /* CSI Control Register 1 */
  49. #define CSISR 0x08 /* CSI Status Register */
  50. #define CSIRXR 0x10 /* CSI RxFIFO Register */
  51. #define CSICR1_RXFF_LEVEL(x) (((x) & 0x3) << 19)
  52. #define CSICR1_SOF_POL (1 << 17)
  53. #define CSICR1_SOF_INTEN (1 << 16)
  54. #define CSICR1_MCLKDIV(x) (((x) & 0xf) << 12)
  55. #define CSICR1_MCLKEN (1 << 9)
  56. #define CSICR1_FCC (1 << 8)
  57. #define CSICR1_BIG_ENDIAN (1 << 7)
  58. #define CSICR1_CLR_RXFIFO (1 << 5)
  59. #define CSICR1_GCLK_MODE (1 << 4)
  60. #define CSICR1_DATA_POL (1 << 2)
  61. #define CSICR1_REDGE (1 << 1)
  62. #define CSICR1_EN (1 << 0)
  63. #define CSISR_SFF_OR_INT (1 << 25)
  64. #define CSISR_RFF_OR_INT (1 << 24)
  65. #define CSISR_STATFF_INT (1 << 21)
  66. #define CSISR_RXFF_INT (1 << 18)
  67. #define CSISR_SOF_INT (1 << 16)
  68. #define CSISR_DRDY (1 << 0)
  69. #define VERSION_CODE KERNEL_VERSION(0, 0, 1)
  70. #define DRIVER_NAME "mx1-camera"
  71. #define CSI_IRQ_MASK (CSISR_SFF_OR_INT | CSISR_RFF_OR_INT | \
  72. CSISR_STATFF_INT | CSISR_RXFF_INT | CSISR_SOF_INT)
  73. #define CSI_BUS_FLAGS (SOCAM_MASTER | SOCAM_HSYNC_ACTIVE_HIGH | \
  74. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW | \
  75. SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING | \
  76. SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_LOW | \
  77. SOCAM_DATAWIDTH_8)
  78. #define MAX_VIDEO_MEM 16 /* Video memory limit in megabytes */
  79. /*
  80. * Structures
  81. */
  82. /* buffer for one video frame */
  83. struct mx1_buffer {
  84. /* common v4l buffer stuff -- must be first */
  85. struct videobuf_buffer vb;
  86. const struct soc_camera_data_format *fmt;
  87. int inwork;
  88. };
  89. /* i.MX1/i.MXL is only supposed to handle one camera on its Camera Sensor
  90. * Interface. If anyone ever builds hardware to enable more than
  91. * one camera, they will have to modify this driver too */
  92. struct mx1_camera_dev {
  93. struct soc_camera_host soc_host;
  94. struct soc_camera_device *icd;
  95. struct mx1_camera_pdata *pdata;
  96. struct mx1_buffer *active;
  97. struct resource *res;
  98. struct clk *clk;
  99. struct list_head capture;
  100. void __iomem *base;
  101. int dma_chan;
  102. unsigned int irq;
  103. unsigned long mclk;
  104. spinlock_t lock;
  105. };
  106. /*
  107. * Videobuf operations
  108. */
  109. static int mx1_videobuf_setup(struct videobuf_queue *vq, unsigned int *count,
  110. unsigned int *size)
  111. {
  112. struct soc_camera_device *icd = vq->priv_data;
  113. *size = icd->user_width * icd->user_height *
  114. ((icd->current_fmt->depth + 7) >> 3);
  115. if (!*count)
  116. *count = 32;
  117. while (*size * *count > MAX_VIDEO_MEM * 1024 * 1024)
  118. (*count)--;
  119. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  120. return 0;
  121. }
  122. static void free_buffer(struct videobuf_queue *vq, struct mx1_buffer *buf)
  123. {
  124. struct soc_camera_device *icd = vq->priv_data;
  125. struct videobuf_buffer *vb = &buf->vb;
  126. BUG_ON(in_interrupt());
  127. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  128. vb, vb->baddr, vb->bsize);
  129. /* This waits until this buffer is out of danger, i.e., until it is no
  130. * longer in STATE_QUEUED or STATE_ACTIVE */
  131. videobuf_waiton(vb, 0, 0);
  132. videobuf_dma_contig_free(vq, vb);
  133. vb->state = VIDEOBUF_NEEDS_INIT;
  134. }
  135. static int mx1_videobuf_prepare(struct videobuf_queue *vq,
  136. struct videobuf_buffer *vb, enum v4l2_field field)
  137. {
  138. struct soc_camera_device *icd = vq->priv_data;
  139. struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
  140. int ret;
  141. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  142. vb, vb->baddr, vb->bsize);
  143. /* Added list head initialization on alloc */
  144. WARN_ON(!list_empty(&vb->queue));
  145. BUG_ON(NULL == icd->current_fmt);
  146. /* I think, in buf_prepare you only have to protect global data,
  147. * the actual buffer is yours */
  148. buf->inwork = 1;
  149. if (buf->fmt != icd->current_fmt ||
  150. vb->width != icd->user_width ||
  151. vb->height != icd->user_height ||
  152. vb->field != field) {
  153. buf->fmt = icd->current_fmt;
  154. vb->width = icd->user_width;
  155. vb->height = icd->user_height;
  156. vb->field = field;
  157. vb->state = VIDEOBUF_NEEDS_INIT;
  158. }
  159. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  160. if (0 != vb->baddr && vb->bsize < vb->size) {
  161. ret = -EINVAL;
  162. goto out;
  163. }
  164. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  165. ret = videobuf_iolock(vq, vb, NULL);
  166. if (ret)
  167. goto fail;
  168. vb->state = VIDEOBUF_PREPARED;
  169. }
  170. buf->inwork = 0;
  171. return 0;
  172. fail:
  173. free_buffer(vq, buf);
  174. out:
  175. buf->inwork = 0;
  176. return ret;
  177. }
  178. static int mx1_camera_setup_dma(struct mx1_camera_dev *pcdev)
  179. {
  180. struct videobuf_buffer *vbuf = &pcdev->active->vb;
  181. struct device *dev = pcdev->icd->dev.parent;
  182. int ret;
  183. if (unlikely(!pcdev->active)) {
  184. dev_err(dev, "DMA End IRQ with no active buffer\n");
  185. return -EFAULT;
  186. }
  187. /* setup sg list for future DMA */
  188. ret = imx_dma_setup_single(pcdev->dma_chan,
  189. videobuf_to_dma_contig(vbuf),
  190. vbuf->size, pcdev->res->start +
  191. CSIRXR, DMA_MODE_READ);
  192. if (unlikely(ret))
  193. dev_err(dev, "Failed to setup DMA sg list\n");
  194. return ret;
  195. }
  196. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  197. static void mx1_videobuf_queue(struct videobuf_queue *vq,
  198. struct videobuf_buffer *vb)
  199. {
  200. struct soc_camera_device *icd = vq->priv_data;
  201. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  202. struct mx1_camera_dev *pcdev = ici->priv;
  203. struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
  204. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  205. vb, vb->baddr, vb->bsize);
  206. list_add_tail(&vb->queue, &pcdev->capture);
  207. vb->state = VIDEOBUF_ACTIVE;
  208. if (!pcdev->active) {
  209. pcdev->active = buf;
  210. /* setup sg list for future DMA */
  211. if (!mx1_camera_setup_dma(pcdev)) {
  212. unsigned int temp;
  213. /* enable SOF irq */
  214. temp = __raw_readl(pcdev->base + CSICR1) |
  215. CSICR1_SOF_INTEN;
  216. __raw_writel(temp, pcdev->base + CSICR1);
  217. }
  218. }
  219. }
  220. static void mx1_videobuf_release(struct videobuf_queue *vq,
  221. struct videobuf_buffer *vb)
  222. {
  223. struct mx1_buffer *buf = container_of(vb, struct mx1_buffer, vb);
  224. #ifdef DEBUG
  225. struct soc_camera_device *icd = vq->priv_data;
  226. struct device *dev = icd->dev.parent;
  227. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  228. vb, vb->baddr, vb->bsize);
  229. switch (vb->state) {
  230. case VIDEOBUF_ACTIVE:
  231. dev_dbg(dev, "%s (active)\n", __func__);
  232. break;
  233. case VIDEOBUF_QUEUED:
  234. dev_dbg(dev, "%s (queued)\n", __func__);
  235. break;
  236. case VIDEOBUF_PREPARED:
  237. dev_dbg(dev, "%s (prepared)\n", __func__);
  238. break;
  239. default:
  240. dev_dbg(dev, "%s (unknown)\n", __func__);
  241. break;
  242. }
  243. #endif
  244. free_buffer(vq, buf);
  245. }
  246. static void mx1_camera_wakeup(struct mx1_camera_dev *pcdev,
  247. struct videobuf_buffer *vb,
  248. struct mx1_buffer *buf)
  249. {
  250. /* _init is used to debug races, see comment in mx1_camera_reqbufs() */
  251. list_del_init(&vb->queue);
  252. vb->state = VIDEOBUF_DONE;
  253. do_gettimeofday(&vb->ts);
  254. vb->field_count++;
  255. wake_up(&vb->done);
  256. if (list_empty(&pcdev->capture)) {
  257. pcdev->active = NULL;
  258. return;
  259. }
  260. pcdev->active = list_entry(pcdev->capture.next,
  261. struct mx1_buffer, vb.queue);
  262. /* setup sg list for future DMA */
  263. if (likely(!mx1_camera_setup_dma(pcdev))) {
  264. unsigned int temp;
  265. /* enable SOF irq */
  266. temp = __raw_readl(pcdev->base + CSICR1) | CSICR1_SOF_INTEN;
  267. __raw_writel(temp, pcdev->base + CSICR1);
  268. }
  269. }
  270. static void mx1_camera_dma_irq(int channel, void *data)
  271. {
  272. struct mx1_camera_dev *pcdev = data;
  273. struct device *dev = pcdev->icd->dev.parent;
  274. struct mx1_buffer *buf;
  275. struct videobuf_buffer *vb;
  276. unsigned long flags;
  277. spin_lock_irqsave(&pcdev->lock, flags);
  278. imx_dma_disable(channel);
  279. if (unlikely(!pcdev->active)) {
  280. dev_err(dev, "DMA End IRQ with no active buffer\n");
  281. goto out;
  282. }
  283. vb = &pcdev->active->vb;
  284. buf = container_of(vb, struct mx1_buffer, vb);
  285. WARN_ON(buf->inwork || list_empty(&vb->queue));
  286. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %d\n", __func__,
  287. vb, vb->baddr, vb->bsize);
  288. mx1_camera_wakeup(pcdev, vb, buf);
  289. out:
  290. spin_unlock_irqrestore(&pcdev->lock, flags);
  291. }
  292. static struct videobuf_queue_ops mx1_videobuf_ops = {
  293. .buf_setup = mx1_videobuf_setup,
  294. .buf_prepare = mx1_videobuf_prepare,
  295. .buf_queue = mx1_videobuf_queue,
  296. .buf_release = mx1_videobuf_release,
  297. };
  298. static void mx1_camera_init_videobuf(struct videobuf_queue *q,
  299. struct soc_camera_device *icd)
  300. {
  301. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  302. struct mx1_camera_dev *pcdev = ici->priv;
  303. videobuf_queue_dma_contig_init(q, &mx1_videobuf_ops, icd->dev.parent,
  304. &pcdev->lock,
  305. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  306. V4L2_FIELD_NONE,
  307. sizeof(struct mx1_buffer), icd);
  308. }
  309. static int mclk_get_divisor(struct mx1_camera_dev *pcdev)
  310. {
  311. unsigned int mclk = pcdev->mclk;
  312. unsigned long div;
  313. unsigned long lcdclk;
  314. lcdclk = clk_get_rate(pcdev->clk);
  315. /* We verify platform_mclk_10khz != 0, so if anyone breaks it, here
  316. * they get a nice Oops */
  317. div = (lcdclk + 2 * mclk - 1) / (2 * mclk) - 1;
  318. dev_dbg(pcdev->icd->dev.parent,
  319. "System clock %lukHz, target freq %dkHz, divisor %lu\n",
  320. lcdclk / 1000, mclk / 1000, div);
  321. return div;
  322. }
  323. static void mx1_camera_activate(struct mx1_camera_dev *pcdev)
  324. {
  325. unsigned int csicr1 = CSICR1_EN;
  326. dev_dbg(pcdev->icd->dev.parent, "Activate device\n");
  327. clk_enable(pcdev->clk);
  328. /* enable CSI before doing anything else */
  329. __raw_writel(csicr1, pcdev->base + CSICR1);
  330. csicr1 |= CSICR1_MCLKEN | CSICR1_FCC | CSICR1_GCLK_MODE;
  331. csicr1 |= CSICR1_MCLKDIV(mclk_get_divisor(pcdev));
  332. csicr1 |= CSICR1_RXFF_LEVEL(2); /* 16 words */
  333. __raw_writel(csicr1, pcdev->base + CSICR1);
  334. }
  335. static void mx1_camera_deactivate(struct mx1_camera_dev *pcdev)
  336. {
  337. dev_dbg(pcdev->icd->dev.parent, "Deactivate device\n");
  338. /* Disable all CSI interface */
  339. __raw_writel(0x00, pcdev->base + CSICR1);
  340. clk_disable(pcdev->clk);
  341. }
  342. /* The following two functions absolutely depend on the fact, that
  343. * there can be only one camera on i.MX1/i.MXL camera sensor interface */
  344. static int mx1_camera_add_device(struct soc_camera_device *icd)
  345. {
  346. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  347. struct mx1_camera_dev *pcdev = ici->priv;
  348. int ret;
  349. if (pcdev->icd) {
  350. ret = -EBUSY;
  351. goto ebusy;
  352. }
  353. dev_info(icd->dev.parent, "MX1 Camera driver attached to camera %d\n",
  354. icd->devnum);
  355. mx1_camera_activate(pcdev);
  356. pcdev->icd = icd;
  357. ebusy:
  358. return ret;
  359. }
  360. static void mx1_camera_remove_device(struct soc_camera_device *icd)
  361. {
  362. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  363. struct mx1_camera_dev *pcdev = ici->priv;
  364. unsigned int csicr1;
  365. BUG_ON(icd != pcdev->icd);
  366. /* disable interrupts */
  367. csicr1 = __raw_readl(pcdev->base + CSICR1) & ~CSI_IRQ_MASK;
  368. __raw_writel(csicr1, pcdev->base + CSICR1);
  369. /* Stop DMA engine */
  370. imx_dma_disable(pcdev->dma_chan);
  371. dev_info(icd->dev.parent, "MX1 Camera driver detached from camera %d\n",
  372. icd->devnum);
  373. mx1_camera_deactivate(pcdev);
  374. pcdev->icd = NULL;
  375. }
  376. static int mx1_camera_set_crop(struct soc_camera_device *icd,
  377. struct v4l2_crop *a)
  378. {
  379. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  380. return v4l2_subdev_call(sd, video, s_crop, a);
  381. }
  382. static int mx1_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  383. {
  384. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  385. struct mx1_camera_dev *pcdev = ici->priv;
  386. unsigned long camera_flags, common_flags;
  387. unsigned int csicr1;
  388. int ret;
  389. camera_flags = icd->ops->query_bus_param(icd);
  390. /* MX1 supports only 8bit buswidth */
  391. common_flags = soc_camera_bus_param_compatible(camera_flags,
  392. CSI_BUS_FLAGS);
  393. if (!common_flags)
  394. return -EINVAL;
  395. icd->buswidth = 8;
  396. /* Make choises, based on platform choice */
  397. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  398. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  399. if (!pcdev->pdata ||
  400. pcdev->pdata->flags & MX1_CAMERA_VSYNC_HIGH)
  401. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  402. else
  403. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  404. }
  405. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  406. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  407. if (!pcdev->pdata ||
  408. pcdev->pdata->flags & MX1_CAMERA_PCLK_RISING)
  409. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  410. else
  411. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  412. }
  413. if ((common_flags & SOCAM_DATA_ACTIVE_HIGH) &&
  414. (common_flags & SOCAM_DATA_ACTIVE_LOW)) {
  415. if (!pcdev->pdata ||
  416. pcdev->pdata->flags & MX1_CAMERA_DATA_HIGH)
  417. common_flags &= ~SOCAM_DATA_ACTIVE_LOW;
  418. else
  419. common_flags &= ~SOCAM_DATA_ACTIVE_HIGH;
  420. }
  421. ret = icd->ops->set_bus_param(icd, common_flags);
  422. if (ret < 0)
  423. return ret;
  424. csicr1 = __raw_readl(pcdev->base + CSICR1);
  425. if (common_flags & SOCAM_PCLK_SAMPLE_RISING)
  426. csicr1 |= CSICR1_REDGE;
  427. if (common_flags & SOCAM_VSYNC_ACTIVE_HIGH)
  428. csicr1 |= CSICR1_SOF_POL;
  429. if (common_flags & SOCAM_DATA_ACTIVE_LOW)
  430. csicr1 |= CSICR1_DATA_POL;
  431. __raw_writel(csicr1, pcdev->base + CSICR1);
  432. return 0;
  433. }
  434. static int mx1_camera_set_fmt(struct soc_camera_device *icd,
  435. struct v4l2_format *f)
  436. {
  437. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  438. const struct soc_camera_format_xlate *xlate;
  439. struct v4l2_pix_format *pix = &f->fmt.pix;
  440. int ret;
  441. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  442. if (!xlate) {
  443. dev_warn(icd->dev.parent, "Format %x not found\n",
  444. pix->pixelformat);
  445. return -EINVAL;
  446. }
  447. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  448. if (!ret) {
  449. icd->buswidth = xlate->buswidth;
  450. icd->current_fmt = xlate->host_fmt;
  451. }
  452. return ret;
  453. }
  454. static int mx1_camera_try_fmt(struct soc_camera_device *icd,
  455. struct v4l2_format *f)
  456. {
  457. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  458. /* TODO: limit to mx1 hardware capabilities */
  459. /* limit to sensor capabilities */
  460. return v4l2_subdev_call(sd, video, try_fmt, f);
  461. }
  462. static int mx1_camera_reqbufs(struct soc_camera_file *icf,
  463. struct v4l2_requestbuffers *p)
  464. {
  465. int i;
  466. /* This is for locking debugging only. I removed spinlocks and now I
  467. * check whether .prepare is ever called on a linked buffer, or whether
  468. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  469. * it hadn't triggered */
  470. for (i = 0; i < p->count; i++) {
  471. struct mx1_buffer *buf = container_of(icf->vb_vidq.bufs[i],
  472. struct mx1_buffer, vb);
  473. buf->inwork = 0;
  474. INIT_LIST_HEAD(&buf->vb.queue);
  475. }
  476. return 0;
  477. }
  478. static unsigned int mx1_camera_poll(struct file *file, poll_table *pt)
  479. {
  480. struct soc_camera_file *icf = file->private_data;
  481. struct mx1_buffer *buf;
  482. buf = list_entry(icf->vb_vidq.stream.next, struct mx1_buffer,
  483. vb.stream);
  484. poll_wait(file, &buf->vb.done, pt);
  485. if (buf->vb.state == VIDEOBUF_DONE ||
  486. buf->vb.state == VIDEOBUF_ERROR)
  487. return POLLIN | POLLRDNORM;
  488. return 0;
  489. }
  490. static int mx1_camera_querycap(struct soc_camera_host *ici,
  491. struct v4l2_capability *cap)
  492. {
  493. /* cap->name is set by the friendly caller:-> */
  494. strlcpy(cap->card, "i.MX1/i.MXL Camera", sizeof(cap->card));
  495. cap->version = VERSION_CODE;
  496. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  497. return 0;
  498. }
  499. static struct soc_camera_host_ops mx1_soc_camera_host_ops = {
  500. .owner = THIS_MODULE,
  501. .add = mx1_camera_add_device,
  502. .remove = mx1_camera_remove_device,
  503. .set_bus_param = mx1_camera_set_bus_param,
  504. .set_crop = mx1_camera_set_crop,
  505. .set_fmt = mx1_camera_set_fmt,
  506. .try_fmt = mx1_camera_try_fmt,
  507. .init_videobuf = mx1_camera_init_videobuf,
  508. .reqbufs = mx1_camera_reqbufs,
  509. .poll = mx1_camera_poll,
  510. .querycap = mx1_camera_querycap,
  511. };
  512. static struct fiq_handler fh = {
  513. .name = "csi_sof"
  514. };
  515. static int __init mx1_camera_probe(struct platform_device *pdev)
  516. {
  517. struct mx1_camera_dev *pcdev;
  518. struct resource *res;
  519. struct pt_regs regs;
  520. struct clk *clk;
  521. void __iomem *base;
  522. unsigned int irq;
  523. int err = 0;
  524. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  525. irq = platform_get_irq(pdev, 0);
  526. if (!res || !irq) {
  527. err = -ENODEV;
  528. goto exit;
  529. }
  530. clk = clk_get(&pdev->dev, "csi_clk");
  531. if (IS_ERR(clk)) {
  532. err = PTR_ERR(clk);
  533. goto exit;
  534. }
  535. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  536. if (!pcdev) {
  537. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  538. err = -ENOMEM;
  539. goto exit_put_clk;
  540. }
  541. pcdev->res = res;
  542. pcdev->clk = clk;
  543. pcdev->pdata = pdev->dev.platform_data;
  544. if (pcdev->pdata)
  545. pcdev->mclk = pcdev->pdata->mclk_10khz * 10000;
  546. if (!pcdev->mclk) {
  547. dev_warn(&pdev->dev,
  548. "mclk_10khz == 0! Please, fix your platform data. "
  549. "Using default 20MHz\n");
  550. pcdev->mclk = 20000000;
  551. }
  552. INIT_LIST_HEAD(&pcdev->capture);
  553. spin_lock_init(&pcdev->lock);
  554. /*
  555. * Request the regions.
  556. */
  557. if (!request_mem_region(res->start, resource_size(res), DRIVER_NAME)) {
  558. err = -EBUSY;
  559. goto exit_kfree;
  560. }
  561. base = ioremap(res->start, resource_size(res));
  562. if (!base) {
  563. err = -ENOMEM;
  564. goto exit_release;
  565. }
  566. pcdev->irq = irq;
  567. pcdev->base = base;
  568. /* request dma */
  569. pcdev->dma_chan = imx_dma_request_by_prio(DRIVER_NAME, DMA_PRIO_HIGH);
  570. if (pcdev->dma_chan < 0) {
  571. dev_err(&pdev->dev, "Can't request DMA for MX1 CSI\n");
  572. err = -EBUSY;
  573. goto exit_iounmap;
  574. }
  575. dev_dbg(&pdev->dev, "got DMA channel %d\n", pcdev->dma_chan);
  576. imx_dma_setup_handlers(pcdev->dma_chan, mx1_camera_dma_irq, NULL,
  577. pcdev);
  578. imx_dma_config_channel(pcdev->dma_chan, IMX_DMA_TYPE_FIFO,
  579. IMX_DMA_MEMSIZE_32, DMA_REQ_CSI_R, 0);
  580. /* burst length : 16 words = 64 bytes */
  581. imx_dma_config_burstlen(pcdev->dma_chan, 0);
  582. /* request irq */
  583. err = claim_fiq(&fh);
  584. if (err) {
  585. dev_err(&pdev->dev, "Camera interrupt register failed \n");
  586. goto exit_free_dma;
  587. }
  588. set_fiq_handler(&mx1_camera_sof_fiq_start, &mx1_camera_sof_fiq_end -
  589. &mx1_camera_sof_fiq_start);
  590. regs.ARM_r8 = DMA_BASE + DMA_DIMR;
  591. regs.ARM_r9 = DMA_BASE + DMA_CCR(pcdev->dma_chan);
  592. regs.ARM_r10 = (long)pcdev->base + CSICR1;
  593. regs.ARM_fp = (long)pcdev->base + CSISR;
  594. regs.ARM_sp = 1 << pcdev->dma_chan;
  595. set_fiq_regs(&regs);
  596. mxc_set_irq_fiq(irq, 1);
  597. enable_fiq(irq);
  598. pcdev->soc_host.drv_name = DRIVER_NAME;
  599. pcdev->soc_host.ops = &mx1_soc_camera_host_ops;
  600. pcdev->soc_host.priv = pcdev;
  601. pcdev->soc_host.v4l2_dev.dev = &pdev->dev;
  602. pcdev->soc_host.nr = pdev->id;
  603. err = soc_camera_host_register(&pcdev->soc_host);
  604. if (err)
  605. goto exit_free_irq;
  606. dev_info(&pdev->dev, "MX1 Camera driver loaded\n");
  607. return 0;
  608. exit_free_irq:
  609. disable_fiq(irq);
  610. mxc_set_irq_fiq(irq, 0);
  611. release_fiq(&fh);
  612. exit_free_dma:
  613. imx_dma_free(pcdev->dma_chan);
  614. exit_iounmap:
  615. iounmap(base);
  616. exit_release:
  617. release_mem_region(res->start, resource_size(res));
  618. exit_kfree:
  619. kfree(pcdev);
  620. exit_put_clk:
  621. clk_put(clk);
  622. exit:
  623. return err;
  624. }
  625. static int __exit mx1_camera_remove(struct platform_device *pdev)
  626. {
  627. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  628. struct mx1_camera_dev *pcdev = container_of(soc_host,
  629. struct mx1_camera_dev, soc_host);
  630. struct resource *res;
  631. imx_dma_free(pcdev->dma_chan);
  632. disable_fiq(pcdev->irq);
  633. mxc_set_irq_fiq(pcdev->irq, 0);
  634. release_fiq(&fh);
  635. clk_put(pcdev->clk);
  636. soc_camera_host_unregister(soc_host);
  637. iounmap(pcdev->base);
  638. res = pcdev->res;
  639. release_mem_region(res->start, resource_size(res));
  640. kfree(pcdev);
  641. dev_info(&pdev->dev, "MX1 Camera driver unloaded\n");
  642. return 0;
  643. }
  644. static struct platform_driver mx1_camera_driver = {
  645. .driver = {
  646. .name = DRIVER_NAME,
  647. },
  648. .remove = __exit_p(mx1_camera_remove),
  649. };
  650. static int __init mx1_camera_init(void)
  651. {
  652. return platform_driver_probe(&mx1_camera_driver, mx1_camera_probe);
  653. }
  654. static void __exit mx1_camera_exit(void)
  655. {
  656. return platform_driver_unregister(&mx1_camera_driver);
  657. }
  658. module_init(mx1_camera_init);
  659. module_exit(mx1_camera_exit);
  660. MODULE_DESCRIPTION("i.MX1/i.MXL SoC Camera Host driver");
  661. MODULE_AUTHOR("Paulius Zaleckas <paulius.zaleckas@teltonika.lt>");
  662. MODULE_LICENSE("GPL v2");
  663. MODULE_ALIAS("platform:" DRIVER_NAME);