mx1_camera.c 22 KB

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