mx1_camera.c 21 KB

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