mx3_camera.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*
  2. * V4L2 Driver for i.MX3x camera host
  3. *
  4. * Copyright (C) 2008
  5. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/videodev2.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/clk.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/sched.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/v4l2-dev.h>
  21. #include <media/videobuf2-dma-contig.h>
  22. #include <media/soc_camera.h>
  23. #include <media/soc_mediabus.h>
  24. #include <mach/ipu.h>
  25. #include <mach/mx3_camera.h>
  26. #include <mach/dma.h>
  27. #define MX3_CAM_DRV_NAME "mx3-camera"
  28. /* CMOS Sensor Interface Registers */
  29. #define CSI_REG_START 0x60
  30. #define CSI_SENS_CONF (0x60 - CSI_REG_START)
  31. #define CSI_SENS_FRM_SIZE (0x64 - CSI_REG_START)
  32. #define CSI_ACT_FRM_SIZE (0x68 - CSI_REG_START)
  33. #define CSI_OUT_FRM_CTRL (0x6C - CSI_REG_START)
  34. #define CSI_TST_CTRL (0x70 - CSI_REG_START)
  35. #define CSI_CCIR_CODE_1 (0x74 - CSI_REG_START)
  36. #define CSI_CCIR_CODE_2 (0x78 - CSI_REG_START)
  37. #define CSI_CCIR_CODE_3 (0x7C - CSI_REG_START)
  38. #define CSI_FLASH_STROBE_1 (0x80 - CSI_REG_START)
  39. #define CSI_FLASH_STROBE_2 (0x84 - CSI_REG_START)
  40. #define CSI_SENS_CONF_VSYNC_POL_SHIFT 0
  41. #define CSI_SENS_CONF_HSYNC_POL_SHIFT 1
  42. #define CSI_SENS_CONF_DATA_POL_SHIFT 2
  43. #define CSI_SENS_CONF_PIX_CLK_POL_SHIFT 3
  44. #define CSI_SENS_CONF_SENS_PRTCL_SHIFT 4
  45. #define CSI_SENS_CONF_SENS_CLKSRC_SHIFT 7
  46. #define CSI_SENS_CONF_DATA_FMT_SHIFT 8
  47. #define CSI_SENS_CONF_DATA_WIDTH_SHIFT 10
  48. #define CSI_SENS_CONF_EXT_VSYNC_SHIFT 15
  49. #define CSI_SENS_CONF_DIVRATIO_SHIFT 16
  50. #define CSI_SENS_CONF_DATA_FMT_RGB_YUV444 (0UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
  51. #define CSI_SENS_CONF_DATA_FMT_YUV422 (2UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
  52. #define CSI_SENS_CONF_DATA_FMT_BAYER (3UL << CSI_SENS_CONF_DATA_FMT_SHIFT)
  53. #define MAX_VIDEO_MEM 16
  54. enum csi_buffer_state {
  55. CSI_BUF_NEEDS_INIT,
  56. CSI_BUF_PREPARED,
  57. };
  58. struct mx3_camera_buffer {
  59. /* common v4l buffer stuff -- must be first */
  60. struct vb2_buffer vb;
  61. enum csi_buffer_state state;
  62. struct list_head queue;
  63. /* One descriptot per scatterlist (per frame) */
  64. struct dma_async_tx_descriptor *txd;
  65. /* We have to "build" a scatterlist ourselves - one element per frame */
  66. struct scatterlist sg;
  67. };
  68. /**
  69. * struct mx3_camera_dev - i.MX3x camera (CSI) object
  70. * @dev: camera device, to which the coherent buffer is attached
  71. * @icd: currently attached camera sensor
  72. * @clk: pointer to clock
  73. * @base: remapped register base address
  74. * @pdata: platform data
  75. * @platform_flags: platform flags
  76. * @mclk: master clock frequency in Hz
  77. * @capture: list of capture videobuffers
  78. * @lock: protects video buffer lists
  79. * @active: active video buffer
  80. * @idmac_channel: array of pointers to IPU DMAC DMA channels
  81. * @soc_host: embedded soc_host object
  82. */
  83. struct mx3_camera_dev {
  84. /*
  85. * i.MX3x is only supposed to handle one camera on its Camera Sensor
  86. * Interface. If anyone ever builds hardware to enable more than one
  87. * camera _simultaneously_, they will have to modify this driver too
  88. */
  89. struct soc_camera_device *icd;
  90. struct clk *clk;
  91. void __iomem *base;
  92. struct mx3_camera_pdata *pdata;
  93. unsigned long platform_flags;
  94. unsigned long mclk;
  95. u16 width_flags; /* max 15 bits */
  96. struct list_head capture;
  97. spinlock_t lock; /* Protects video buffer lists */
  98. struct mx3_camera_buffer *active;
  99. size_t buf_total;
  100. struct vb2_alloc_ctx *alloc_ctx;
  101. enum v4l2_field field;
  102. int sequence;
  103. /* IDMAC / dmaengine interface */
  104. struct idmac_channel *idmac_channel[1]; /* We need one channel */
  105. struct soc_camera_host soc_host;
  106. };
  107. struct dma_chan_request {
  108. struct mx3_camera_dev *mx3_cam;
  109. enum ipu_channel id;
  110. };
  111. static u32 csi_reg_read(struct mx3_camera_dev *mx3, off_t reg)
  112. {
  113. return __raw_readl(mx3->base + reg);
  114. }
  115. static void csi_reg_write(struct mx3_camera_dev *mx3, u32 value, off_t reg)
  116. {
  117. __raw_writel(value, mx3->base + reg);
  118. }
  119. static struct mx3_camera_buffer *to_mx3_vb(struct vb2_buffer *vb)
  120. {
  121. return container_of(vb, struct mx3_camera_buffer, vb);
  122. }
  123. /* Called from the IPU IDMAC ISR */
  124. static void mx3_cam_dma_done(void *arg)
  125. {
  126. struct idmac_tx_desc *desc = to_tx_desc(arg);
  127. struct dma_chan *chan = desc->txd.chan;
  128. struct idmac_channel *ichannel = to_idmac_chan(chan);
  129. struct mx3_camera_dev *mx3_cam = ichannel->client;
  130. dev_dbg(chan->device->dev, "callback cookie %d, active DMA 0x%08x\n",
  131. desc->txd.cookie, mx3_cam->active ? sg_dma_address(&mx3_cam->active->sg) : 0);
  132. spin_lock(&mx3_cam->lock);
  133. if (mx3_cam->active) {
  134. struct vb2_buffer *vb = &mx3_cam->active->vb;
  135. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  136. list_del_init(&buf->queue);
  137. do_gettimeofday(&vb->v4l2_buf.timestamp);
  138. vb->v4l2_buf.field = mx3_cam->field;
  139. vb->v4l2_buf.sequence = mx3_cam->sequence++;
  140. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  141. }
  142. if (list_empty(&mx3_cam->capture)) {
  143. mx3_cam->active = NULL;
  144. spin_unlock(&mx3_cam->lock);
  145. /*
  146. * stop capture - without further buffers IPU_CHA_BUF0_RDY will
  147. * not get updated
  148. */
  149. return;
  150. }
  151. mx3_cam->active = list_entry(mx3_cam->capture.next,
  152. struct mx3_camera_buffer, queue);
  153. spin_unlock(&mx3_cam->lock);
  154. }
  155. /*
  156. * Videobuf operations
  157. */
  158. /*
  159. * Calculate the __buffer__ (not data) size and number of buffers.
  160. */
  161. static int mx3_videobuf_setup(struct vb2_queue *vq,
  162. const struct v4l2_format *fmt,
  163. unsigned int *count, unsigned int *num_planes,
  164. unsigned int sizes[], void *alloc_ctxs[])
  165. {
  166. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  167. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  168. struct mx3_camera_dev *mx3_cam = ici->priv;
  169. int bytes_per_line;
  170. unsigned int height;
  171. if (!mx3_cam->idmac_channel[0])
  172. return -EINVAL;
  173. if (fmt) {
  174. const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
  175. fmt->fmt.pix.pixelformat);
  176. if (!xlate)
  177. return -EINVAL;
  178. bytes_per_line = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
  179. xlate->host_fmt);
  180. height = fmt->fmt.pix.height;
  181. } else {
  182. /* Called from VIDIOC_REQBUFS or in compatibility mode */
  183. bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  184. icd->current_fmt->host_fmt);
  185. height = icd->user_height;
  186. }
  187. if (bytes_per_line < 0)
  188. return bytes_per_line;
  189. sizes[0] = bytes_per_line * height;
  190. alloc_ctxs[0] = mx3_cam->alloc_ctx;
  191. if (!vq->num_buffers)
  192. mx3_cam->sequence = 0;
  193. if (!*count)
  194. *count = 2;
  195. /* If *num_planes != 0, we have already verified *count. */
  196. if (!*num_planes &&
  197. sizes[0] * *count + mx3_cam->buf_total > MAX_VIDEO_MEM * 1024 * 1024)
  198. *count = (MAX_VIDEO_MEM * 1024 * 1024 - mx3_cam->buf_total) /
  199. sizes[0];
  200. *num_planes = 1;
  201. return 0;
  202. }
  203. static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
  204. {
  205. /* Add more formats as need arises and test possibilities appear... */
  206. switch (fourcc) {
  207. case V4L2_PIX_FMT_RGB24:
  208. return IPU_PIX_FMT_RGB24;
  209. case V4L2_PIX_FMT_UYVY:
  210. case V4L2_PIX_FMT_RGB565:
  211. default:
  212. return IPU_PIX_FMT_GENERIC;
  213. }
  214. }
  215. static void mx3_videobuf_queue(struct vb2_buffer *vb)
  216. {
  217. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  218. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  219. struct mx3_camera_dev *mx3_cam = ici->priv;
  220. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  221. struct scatterlist *sg = &buf->sg;
  222. struct dma_async_tx_descriptor *txd;
  223. struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
  224. struct idmac_video_param *video = &ichan->params.video;
  225. const struct soc_mbus_pixelfmt *host_fmt = icd->current_fmt->host_fmt;
  226. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width, host_fmt);
  227. unsigned long flags;
  228. dma_cookie_t cookie;
  229. size_t new_size;
  230. BUG_ON(bytes_per_line <= 0);
  231. new_size = bytes_per_line * icd->user_height;
  232. if (vb2_plane_size(vb, 0) < new_size) {
  233. dev_err(icd->parent, "Buffer #%d too small (%lu < %zu)\n",
  234. vb->v4l2_buf.index, vb2_plane_size(vb, 0), new_size);
  235. goto error;
  236. }
  237. if (buf->state == CSI_BUF_NEEDS_INIT) {
  238. sg_dma_address(sg) = vb2_dma_contig_plane_dma_addr(vb, 0);
  239. sg_dma_len(sg) = new_size;
  240. txd = ichan->dma_chan.device->device_prep_slave_sg(
  241. &ichan->dma_chan, sg, 1, DMA_FROM_DEVICE,
  242. DMA_PREP_INTERRUPT);
  243. if (!txd)
  244. goto error;
  245. txd->callback_param = txd;
  246. txd->callback = mx3_cam_dma_done;
  247. buf->state = CSI_BUF_PREPARED;
  248. buf->txd = txd;
  249. } else {
  250. txd = buf->txd;
  251. }
  252. vb2_set_plane_payload(vb, 0, new_size);
  253. /* This is the configuration of one sg-element */
  254. video->out_pixel_fmt = fourcc_to_ipu_pix(host_fmt->fourcc);
  255. if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
  256. /*
  257. * If the IPU DMA channel is configured to transfer generic
  258. * 8-bit data, we have to set up the geometry parameters
  259. * correctly, according to the current pixel format. The DMA
  260. * horizontal parameters in this case are expressed in bytes,
  261. * not in pixels.
  262. */
  263. video->out_width = bytes_per_line;
  264. video->out_height = icd->user_height;
  265. video->out_stride = bytes_per_line;
  266. } else {
  267. /*
  268. * For IPU known formats the pixel unit will be managed
  269. * successfully by the IPU code
  270. */
  271. video->out_width = icd->user_width;
  272. video->out_height = icd->user_height;
  273. video->out_stride = icd->user_width;
  274. }
  275. #ifdef DEBUG
  276. /* helps to see what DMA actually has written */
  277. if (vb2_plane_vaddr(vb, 0))
  278. memset(vb2_plane_vaddr(vb, 0), 0xaa, vb2_get_plane_payload(vb, 0));
  279. #endif
  280. spin_lock_irqsave(&mx3_cam->lock, flags);
  281. list_add_tail(&buf->queue, &mx3_cam->capture);
  282. if (!mx3_cam->active)
  283. mx3_cam->active = buf;
  284. spin_unlock_irq(&mx3_cam->lock);
  285. cookie = txd->tx_submit(txd);
  286. dev_dbg(icd->parent, "Submitted cookie %d DMA 0x%08x\n",
  287. cookie, sg_dma_address(&buf->sg));
  288. if (cookie >= 0)
  289. return;
  290. spin_lock_irq(&mx3_cam->lock);
  291. /* Submit error */
  292. list_del_init(&buf->queue);
  293. if (mx3_cam->active == buf)
  294. mx3_cam->active = NULL;
  295. spin_unlock_irqrestore(&mx3_cam->lock, flags);
  296. error:
  297. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  298. }
  299. static void mx3_videobuf_release(struct vb2_buffer *vb)
  300. {
  301. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  302. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  303. struct mx3_camera_dev *mx3_cam = ici->priv;
  304. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  305. struct dma_async_tx_descriptor *txd = buf->txd;
  306. unsigned long flags;
  307. dev_dbg(icd->parent,
  308. "Release%s DMA 0x%08x, queue %sempty\n",
  309. mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
  310. list_empty(&buf->queue) ? "" : "not ");
  311. spin_lock_irqsave(&mx3_cam->lock, flags);
  312. if (mx3_cam->active == buf)
  313. mx3_cam->active = NULL;
  314. /* Doesn't hurt also if the list is empty */
  315. list_del_init(&buf->queue);
  316. buf->state = CSI_BUF_NEEDS_INIT;
  317. if (txd) {
  318. buf->txd = NULL;
  319. if (mx3_cam->idmac_channel[0])
  320. async_tx_ack(txd);
  321. }
  322. spin_unlock_irqrestore(&mx3_cam->lock, flags);
  323. mx3_cam->buf_total -= vb2_plane_size(vb, 0);
  324. }
  325. static int mx3_videobuf_init(struct vb2_buffer *vb)
  326. {
  327. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  328. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  329. struct mx3_camera_dev *mx3_cam = ici->priv;
  330. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  331. /* This is for locking debugging only */
  332. INIT_LIST_HEAD(&buf->queue);
  333. sg_init_table(&buf->sg, 1);
  334. buf->state = CSI_BUF_NEEDS_INIT;
  335. mx3_cam->buf_total += vb2_plane_size(vb, 0);
  336. return 0;
  337. }
  338. static int mx3_stop_streaming(struct vb2_queue *q)
  339. {
  340. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  341. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  342. struct mx3_camera_dev *mx3_cam = ici->priv;
  343. struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
  344. struct mx3_camera_buffer *buf, *tmp;
  345. unsigned long flags;
  346. if (ichan) {
  347. struct dma_chan *chan = &ichan->dma_chan;
  348. chan->device->device_control(chan, DMA_PAUSE, 0);
  349. }
  350. spin_lock_irqsave(&mx3_cam->lock, flags);
  351. mx3_cam->active = NULL;
  352. list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
  353. list_del_init(&buf->queue);
  354. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  355. }
  356. spin_unlock_irqrestore(&mx3_cam->lock, flags);
  357. return 0;
  358. }
  359. static struct vb2_ops mx3_videobuf_ops = {
  360. .queue_setup = mx3_videobuf_setup,
  361. .buf_queue = mx3_videobuf_queue,
  362. .buf_cleanup = mx3_videobuf_release,
  363. .buf_init = mx3_videobuf_init,
  364. .wait_prepare = soc_camera_unlock,
  365. .wait_finish = soc_camera_lock,
  366. .stop_streaming = mx3_stop_streaming,
  367. };
  368. static int mx3_camera_init_videobuf(struct vb2_queue *q,
  369. struct soc_camera_device *icd)
  370. {
  371. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  372. q->io_modes = VB2_MMAP | VB2_USERPTR;
  373. q->drv_priv = icd;
  374. q->ops = &mx3_videobuf_ops;
  375. q->mem_ops = &vb2_dma_contig_memops;
  376. q->buf_struct_size = sizeof(struct mx3_camera_buffer);
  377. return vb2_queue_init(q);
  378. }
  379. /* First part of ipu_csi_init_interface() */
  380. static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam,
  381. struct soc_camera_device *icd)
  382. {
  383. u32 conf;
  384. long rate;
  385. /* Set default size: ipu_csi_set_window_size() */
  386. csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
  387. /* ...and position to 0:0: ipu_csi_set_window_pos() */
  388. conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
  389. csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
  390. /* We use only gated clock synchronisation mode so far */
  391. conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
  392. /* Set generic data, platform-biggest bus-width */
  393. conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
  394. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
  395. conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  396. else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
  397. conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  398. else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
  399. conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  400. else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
  401. conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  402. if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
  403. conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
  404. if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
  405. conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
  406. if (mx3_cam->platform_flags & MX3_CAMERA_DP)
  407. conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
  408. if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
  409. conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
  410. if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
  411. conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
  412. if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
  413. conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
  414. /* ipu_csi_init_interface() */
  415. csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
  416. clk_enable(mx3_cam->clk);
  417. rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
  418. dev_dbg(icd->parent, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
  419. if (rate)
  420. clk_set_rate(mx3_cam->clk, rate);
  421. }
  422. /* Called with .video_lock held */
  423. static int mx3_camera_add_device(struct soc_camera_device *icd)
  424. {
  425. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  426. struct mx3_camera_dev *mx3_cam = ici->priv;
  427. if (mx3_cam->icd)
  428. return -EBUSY;
  429. mx3_camera_activate(mx3_cam, icd);
  430. mx3_cam->buf_total = 0;
  431. mx3_cam->icd = icd;
  432. dev_info(icd->parent, "MX3 Camera driver attached to camera %d\n",
  433. icd->devnum);
  434. return 0;
  435. }
  436. /* Called with .video_lock held */
  437. static void mx3_camera_remove_device(struct soc_camera_device *icd)
  438. {
  439. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  440. struct mx3_camera_dev *mx3_cam = ici->priv;
  441. struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
  442. BUG_ON(icd != mx3_cam->icd);
  443. if (*ichan) {
  444. dma_release_channel(&(*ichan)->dma_chan);
  445. *ichan = NULL;
  446. }
  447. clk_disable(mx3_cam->clk);
  448. mx3_cam->icd = NULL;
  449. dev_info(icd->parent, "MX3 Camera driver detached from camera %d\n",
  450. icd->devnum);
  451. }
  452. static int test_platform_param(struct mx3_camera_dev *mx3_cam,
  453. unsigned char buswidth, unsigned long *flags)
  454. {
  455. /*
  456. * If requested data width is supported by the platform, use it or any
  457. * possible lower value - i.MX31 is smart enough to shift bits
  458. */
  459. if (buswidth > fls(mx3_cam->width_flags))
  460. return -EINVAL;
  461. /*
  462. * Platform specified synchronization and pixel clock polarities are
  463. * only a recommendation and are only used during probing. MX3x
  464. * camera interface only works in master mode, i.e., uses HSYNC and
  465. * VSYNC signals from the sensor
  466. */
  467. *flags = V4L2_MBUS_MASTER |
  468. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  469. V4L2_MBUS_HSYNC_ACTIVE_LOW |
  470. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  471. V4L2_MBUS_VSYNC_ACTIVE_LOW |
  472. V4L2_MBUS_PCLK_SAMPLE_RISING |
  473. V4L2_MBUS_PCLK_SAMPLE_FALLING |
  474. V4L2_MBUS_DATA_ACTIVE_HIGH |
  475. V4L2_MBUS_DATA_ACTIVE_LOW;
  476. return 0;
  477. }
  478. static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
  479. const unsigned int depth)
  480. {
  481. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  482. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  483. struct mx3_camera_dev *mx3_cam = ici->priv;
  484. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  485. unsigned long bus_flags, common_flags;
  486. int ret = test_platform_param(mx3_cam, depth, &bus_flags);
  487. dev_dbg(icd->parent, "request bus width %d bit: %d\n", depth, ret);
  488. if (ret < 0)
  489. return ret;
  490. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  491. if (!ret) {
  492. common_flags = soc_mbus_config_compatible(&cfg,
  493. bus_flags);
  494. if (!common_flags) {
  495. dev_warn(icd->parent,
  496. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  497. cfg.flags, bus_flags);
  498. return -EINVAL;
  499. }
  500. } else if (ret != -ENOIOCTLCMD) {
  501. return ret;
  502. }
  503. return 0;
  504. }
  505. static bool chan_filter(struct dma_chan *chan, void *arg)
  506. {
  507. struct dma_chan_request *rq = arg;
  508. struct mx3_camera_pdata *pdata;
  509. if (!imx_dma_is_ipu(chan))
  510. return false;
  511. if (!rq)
  512. return false;
  513. pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
  514. return rq->id == chan->chan_id &&
  515. pdata->dma_dev == chan->device->dev;
  516. }
  517. static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
  518. {
  519. .fourcc = V4L2_PIX_FMT_SBGGR8,
  520. .name = "Bayer BGGR (sRGB) 8 bit",
  521. .bits_per_sample = 8,
  522. .packing = SOC_MBUS_PACKING_NONE,
  523. .order = SOC_MBUS_ORDER_LE,
  524. }, {
  525. .fourcc = V4L2_PIX_FMT_GREY,
  526. .name = "Monochrome 8 bit",
  527. .bits_per_sample = 8,
  528. .packing = SOC_MBUS_PACKING_NONE,
  529. .order = SOC_MBUS_ORDER_LE,
  530. },
  531. };
  532. /* This will be corrected as we get more formats */
  533. static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  534. {
  535. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  536. (fmt->bits_per_sample == 8 &&
  537. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  538. (fmt->bits_per_sample > 8 &&
  539. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  540. }
  541. static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
  542. struct soc_camera_format_xlate *xlate)
  543. {
  544. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  545. struct device *dev = icd->parent;
  546. int formats = 0, ret;
  547. enum v4l2_mbus_pixelcode code;
  548. const struct soc_mbus_pixelfmt *fmt;
  549. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  550. if (ret < 0)
  551. /* No more formats */
  552. return 0;
  553. fmt = soc_mbus_get_fmtdesc(code);
  554. if (!fmt) {
  555. dev_warn(icd->parent,
  556. "Unsupported format code #%u: %d\n", idx, code);
  557. return 0;
  558. }
  559. /* This also checks support for the requested bits-per-sample */
  560. ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
  561. if (ret < 0)
  562. return 0;
  563. switch (code) {
  564. case V4L2_MBUS_FMT_SBGGR10_1X10:
  565. formats++;
  566. if (xlate) {
  567. xlate->host_fmt = &mx3_camera_formats[0];
  568. xlate->code = code;
  569. xlate++;
  570. dev_dbg(dev, "Providing format %s using code %d\n",
  571. mx3_camera_formats[0].name, code);
  572. }
  573. break;
  574. case V4L2_MBUS_FMT_Y10_1X10:
  575. formats++;
  576. if (xlate) {
  577. xlate->host_fmt = &mx3_camera_formats[1];
  578. xlate->code = code;
  579. xlate++;
  580. dev_dbg(dev, "Providing format %s using code %d\n",
  581. mx3_camera_formats[1].name, code);
  582. }
  583. break;
  584. default:
  585. if (!mx3_camera_packing_supported(fmt))
  586. return 0;
  587. }
  588. /* Generic pass-through */
  589. formats++;
  590. if (xlate) {
  591. xlate->host_fmt = fmt;
  592. xlate->code = code;
  593. dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
  594. (fmt->fourcc >> (0*8)) & 0xFF,
  595. (fmt->fourcc >> (1*8)) & 0xFF,
  596. (fmt->fourcc >> (2*8)) & 0xFF,
  597. (fmt->fourcc >> (3*8)) & 0xFF);
  598. xlate++;
  599. }
  600. return formats;
  601. }
  602. static void configure_geometry(struct mx3_camera_dev *mx3_cam,
  603. unsigned int width, unsigned int height,
  604. const struct soc_mbus_pixelfmt *fmt)
  605. {
  606. u32 ctrl, width_field, height_field;
  607. if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
  608. /*
  609. * As the CSI will be configured to output BAYER, here
  610. * the width parameter count the number of samples to
  611. * capture to complete the whole image width.
  612. */
  613. unsigned int num, den;
  614. int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
  615. BUG_ON(ret < 0);
  616. width = width * num / den;
  617. }
  618. /* Setup frame size - this cannot be changed on-the-fly... */
  619. width_field = width - 1;
  620. height_field = height - 1;
  621. csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
  622. csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
  623. csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
  624. csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
  625. /* ...and position */
  626. ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
  627. /* Sensor does the cropping */
  628. csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
  629. }
  630. static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
  631. {
  632. dma_cap_mask_t mask;
  633. struct dma_chan *chan;
  634. struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
  635. /* We have to use IDMAC_IC_7 for Bayer / generic data */
  636. struct dma_chan_request rq = {.mx3_cam = mx3_cam,
  637. .id = IDMAC_IC_7};
  638. dma_cap_zero(mask);
  639. dma_cap_set(DMA_SLAVE, mask);
  640. dma_cap_set(DMA_PRIVATE, mask);
  641. chan = dma_request_channel(mask, chan_filter, &rq);
  642. if (!chan)
  643. return -EBUSY;
  644. *ichan = to_idmac_chan(chan);
  645. (*ichan)->client = mx3_cam;
  646. return 0;
  647. }
  648. /*
  649. * FIXME: learn to use stride != width, then we can keep stride properly aligned
  650. * and support arbitrary (even) widths.
  651. */
  652. static inline void stride_align(__u32 *width)
  653. {
  654. if (ALIGN(*width, 8) < 4096)
  655. *width = ALIGN(*width, 8);
  656. else
  657. *width = *width & ~7;
  658. }
  659. /*
  660. * As long as we don't implement host-side cropping and scaling, we can use
  661. * default g_crop and cropcap from soc_camera.c
  662. */
  663. static int mx3_camera_set_crop(struct soc_camera_device *icd,
  664. struct v4l2_crop *a)
  665. {
  666. struct v4l2_rect *rect = &a->c;
  667. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  668. struct mx3_camera_dev *mx3_cam = ici->priv;
  669. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  670. struct v4l2_mbus_framefmt mf;
  671. int ret;
  672. soc_camera_limit_side(&rect->left, &rect->width, 0, 2, 4096);
  673. soc_camera_limit_side(&rect->top, &rect->height, 0, 2, 4096);
  674. ret = v4l2_subdev_call(sd, video, s_crop, a);
  675. if (ret < 0)
  676. return ret;
  677. /* The capture device might have changed its output sizes */
  678. ret = v4l2_subdev_call(sd, video, g_mbus_fmt, &mf);
  679. if (ret < 0)
  680. return ret;
  681. if (mf.code != icd->current_fmt->code)
  682. return -EINVAL;
  683. if (mf.width & 7) {
  684. /* Ouch! We can only handle 8-byte aligned width... */
  685. stride_align(&mf.width);
  686. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  687. if (ret < 0)
  688. return ret;
  689. }
  690. if (mf.width != icd->user_width || mf.height != icd->user_height)
  691. configure_geometry(mx3_cam, mf.width, mf.height,
  692. icd->current_fmt->host_fmt);
  693. dev_dbg(icd->parent, "Sensor cropped %dx%d\n",
  694. mf.width, mf.height);
  695. icd->user_width = mf.width;
  696. icd->user_height = mf.height;
  697. return ret;
  698. }
  699. static int mx3_camera_set_fmt(struct soc_camera_device *icd,
  700. struct v4l2_format *f)
  701. {
  702. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  703. struct mx3_camera_dev *mx3_cam = ici->priv;
  704. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  705. const struct soc_camera_format_xlate *xlate;
  706. struct v4l2_pix_format *pix = &f->fmt.pix;
  707. struct v4l2_mbus_framefmt mf;
  708. int ret;
  709. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  710. if (!xlate) {
  711. dev_warn(icd->parent, "Format %x not found\n",
  712. pix->pixelformat);
  713. return -EINVAL;
  714. }
  715. stride_align(&pix->width);
  716. dev_dbg(icd->parent, "Set format %dx%d\n", pix->width, pix->height);
  717. /*
  718. * Might have to perform a complete interface initialisation like in
  719. * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
  720. * mxc_v4l2_s_fmt()
  721. */
  722. configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
  723. mf.width = pix->width;
  724. mf.height = pix->height;
  725. mf.field = pix->field;
  726. mf.colorspace = pix->colorspace;
  727. mf.code = xlate->code;
  728. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  729. if (ret < 0)
  730. return ret;
  731. if (mf.code != xlate->code)
  732. return -EINVAL;
  733. if (!mx3_cam->idmac_channel[0]) {
  734. ret = acquire_dma_channel(mx3_cam);
  735. if (ret < 0)
  736. return ret;
  737. }
  738. pix->width = mf.width;
  739. pix->height = mf.height;
  740. pix->field = mf.field;
  741. mx3_cam->field = mf.field;
  742. pix->colorspace = mf.colorspace;
  743. icd->current_fmt = xlate;
  744. dev_dbg(icd->parent, "Sensor set %dx%d\n", pix->width, pix->height);
  745. return ret;
  746. }
  747. static int mx3_camera_try_fmt(struct soc_camera_device *icd,
  748. struct v4l2_format *f)
  749. {
  750. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  751. const struct soc_camera_format_xlate *xlate;
  752. struct v4l2_pix_format *pix = &f->fmt.pix;
  753. struct v4l2_mbus_framefmt mf;
  754. __u32 pixfmt = pix->pixelformat;
  755. int ret;
  756. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  757. if (pixfmt && !xlate) {
  758. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  759. return -EINVAL;
  760. }
  761. /* limit to MX3 hardware capabilities */
  762. if (pix->height > 4096)
  763. pix->height = 4096;
  764. if (pix->width > 4096)
  765. pix->width = 4096;
  766. /* limit to sensor capabilities */
  767. mf.width = pix->width;
  768. mf.height = pix->height;
  769. mf.field = pix->field;
  770. mf.colorspace = pix->colorspace;
  771. mf.code = xlate->code;
  772. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  773. if (ret < 0)
  774. return ret;
  775. pix->width = mf.width;
  776. pix->height = mf.height;
  777. pix->colorspace = mf.colorspace;
  778. switch (mf.field) {
  779. case V4L2_FIELD_ANY:
  780. pix->field = V4L2_FIELD_NONE;
  781. break;
  782. case V4L2_FIELD_NONE:
  783. break;
  784. default:
  785. dev_err(icd->parent, "Field type %d unsupported.\n",
  786. mf.field);
  787. ret = -EINVAL;
  788. }
  789. return ret;
  790. }
  791. static int mx3_camera_reqbufs(struct soc_camera_device *icd,
  792. struct v4l2_requestbuffers *p)
  793. {
  794. return 0;
  795. }
  796. static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
  797. {
  798. struct soc_camera_device *icd = file->private_data;
  799. return vb2_poll(&icd->vb2_vidq, file, pt);
  800. }
  801. static int mx3_camera_querycap(struct soc_camera_host *ici,
  802. struct v4l2_capability *cap)
  803. {
  804. /* cap->name is set by the firendly caller:-> */
  805. strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
  806. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  807. return 0;
  808. }
  809. static int mx3_camera_set_bus_param(struct soc_camera_device *icd, __u32 pixfmt)
  810. {
  811. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  812. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  813. struct mx3_camera_dev *mx3_cam = ici->priv;
  814. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  815. unsigned long bus_flags, common_flags;
  816. u32 dw, sens_conf;
  817. const struct soc_mbus_pixelfmt *fmt;
  818. int buswidth;
  819. int ret;
  820. const struct soc_camera_format_xlate *xlate;
  821. struct device *dev = icd->parent;
  822. fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
  823. if (!fmt)
  824. return -EINVAL;
  825. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  826. if (!xlate) {
  827. dev_warn(dev, "Format %x not found\n", pixfmt);
  828. return -EINVAL;
  829. }
  830. buswidth = fmt->bits_per_sample;
  831. ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
  832. dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
  833. if (ret < 0)
  834. return ret;
  835. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  836. if (!ret) {
  837. common_flags = soc_mbus_config_compatible(&cfg,
  838. bus_flags);
  839. if (!common_flags) {
  840. dev_warn(icd->parent,
  841. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  842. cfg.flags, bus_flags);
  843. return -EINVAL;
  844. }
  845. } else if (ret != -ENOIOCTLCMD) {
  846. return ret;
  847. } else {
  848. common_flags = bus_flags;
  849. }
  850. dev_dbg(dev, "Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
  851. cfg.flags, bus_flags, common_flags);
  852. /* Make choices, based on platform preferences */
  853. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  854. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  855. if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
  856. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  857. else
  858. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  859. }
  860. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  861. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  862. if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
  863. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  864. else
  865. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  866. }
  867. if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
  868. (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
  869. if (mx3_cam->platform_flags & MX3_CAMERA_DP)
  870. common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
  871. else
  872. common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
  873. }
  874. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  875. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  876. if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
  877. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  878. else
  879. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  880. }
  881. cfg.flags = common_flags;
  882. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  883. if (ret < 0 && ret != -ENOIOCTLCMD) {
  884. dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
  885. common_flags, ret);
  886. return ret;
  887. }
  888. /*
  889. * So far only gated clock mode is supported. Add a line
  890. * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
  891. * below and select the required mode when supporting other
  892. * synchronisation protocols.
  893. */
  894. sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
  895. ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
  896. (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
  897. (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
  898. (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
  899. (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
  900. (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
  901. /* TODO: Support RGB and YUV formats */
  902. /* This has been set in mx3_camera_activate(), but we clear it above */
  903. sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
  904. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  905. sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
  906. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  907. sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
  908. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  909. sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
  910. if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
  911. sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
  912. /* Just do what we're asked to do */
  913. switch (xlate->host_fmt->bits_per_sample) {
  914. case 4:
  915. dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  916. break;
  917. case 8:
  918. dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  919. break;
  920. case 10:
  921. dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  922. break;
  923. default:
  924. /*
  925. * Actually it can only be 15 now, default is just to silence
  926. * compiler warnings
  927. */
  928. case 15:
  929. dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  930. }
  931. csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
  932. dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
  933. return 0;
  934. }
  935. static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
  936. .owner = THIS_MODULE,
  937. .add = mx3_camera_add_device,
  938. .remove = mx3_camera_remove_device,
  939. .set_crop = mx3_camera_set_crop,
  940. .set_fmt = mx3_camera_set_fmt,
  941. .try_fmt = mx3_camera_try_fmt,
  942. .get_formats = mx3_camera_get_formats,
  943. .init_videobuf2 = mx3_camera_init_videobuf,
  944. .reqbufs = mx3_camera_reqbufs,
  945. .poll = mx3_camera_poll,
  946. .querycap = mx3_camera_querycap,
  947. .set_bus_param = mx3_camera_set_bus_param,
  948. };
  949. static int __devinit mx3_camera_probe(struct platform_device *pdev)
  950. {
  951. struct mx3_camera_dev *mx3_cam;
  952. struct resource *res;
  953. void __iomem *base;
  954. int err = 0;
  955. struct soc_camera_host *soc_host;
  956. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  957. if (!res) {
  958. err = -ENODEV;
  959. goto egetres;
  960. }
  961. mx3_cam = vzalloc(sizeof(*mx3_cam));
  962. if (!mx3_cam) {
  963. dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
  964. err = -ENOMEM;
  965. goto ealloc;
  966. }
  967. mx3_cam->clk = clk_get(&pdev->dev, NULL);
  968. if (IS_ERR(mx3_cam->clk)) {
  969. err = PTR_ERR(mx3_cam->clk);
  970. goto eclkget;
  971. }
  972. mx3_cam->pdata = pdev->dev.platform_data;
  973. mx3_cam->platform_flags = mx3_cam->pdata->flags;
  974. if (!(mx3_cam->platform_flags & (MX3_CAMERA_DATAWIDTH_4 |
  975. MX3_CAMERA_DATAWIDTH_8 | MX3_CAMERA_DATAWIDTH_10 |
  976. MX3_CAMERA_DATAWIDTH_15))) {
  977. /*
  978. * Platform hasn't set available data widths. This is bad.
  979. * Warn and use a default.
  980. */
  981. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  982. "data widths, using default 8 bit\n");
  983. mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
  984. }
  985. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
  986. mx3_cam->width_flags = 1 << 3;
  987. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
  988. mx3_cam->width_flags |= 1 << 7;
  989. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
  990. mx3_cam->width_flags |= 1 << 9;
  991. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
  992. mx3_cam->width_flags |= 1 << 14;
  993. mx3_cam->mclk = mx3_cam->pdata->mclk_10khz * 10000;
  994. if (!mx3_cam->mclk) {
  995. dev_warn(&pdev->dev,
  996. "mclk_10khz == 0! Please, fix your platform data. "
  997. "Using default 20MHz\n");
  998. mx3_cam->mclk = 20000000;
  999. }
  1000. /* list of video-buffers */
  1001. INIT_LIST_HEAD(&mx3_cam->capture);
  1002. spin_lock_init(&mx3_cam->lock);
  1003. base = ioremap(res->start, resource_size(res));
  1004. if (!base) {
  1005. pr_err("Couldn't map %x@%x\n", resource_size(res), res->start);
  1006. err = -ENOMEM;
  1007. goto eioremap;
  1008. }
  1009. mx3_cam->base = base;
  1010. soc_host = &mx3_cam->soc_host;
  1011. soc_host->drv_name = MX3_CAM_DRV_NAME;
  1012. soc_host->ops = &mx3_soc_camera_host_ops;
  1013. soc_host->priv = mx3_cam;
  1014. soc_host->v4l2_dev.dev = &pdev->dev;
  1015. soc_host->nr = pdev->id;
  1016. mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1017. if (IS_ERR(mx3_cam->alloc_ctx)) {
  1018. err = PTR_ERR(mx3_cam->alloc_ctx);
  1019. goto eallocctx;
  1020. }
  1021. err = soc_camera_host_register(soc_host);
  1022. if (err)
  1023. goto ecamhostreg;
  1024. /* IDMAC interface */
  1025. dmaengine_get();
  1026. return 0;
  1027. ecamhostreg:
  1028. vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
  1029. eallocctx:
  1030. iounmap(base);
  1031. eioremap:
  1032. clk_put(mx3_cam->clk);
  1033. eclkget:
  1034. vfree(mx3_cam);
  1035. ealloc:
  1036. egetres:
  1037. return err;
  1038. }
  1039. static int __devexit mx3_camera_remove(struct platform_device *pdev)
  1040. {
  1041. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1042. struct mx3_camera_dev *mx3_cam = container_of(soc_host,
  1043. struct mx3_camera_dev, soc_host);
  1044. clk_put(mx3_cam->clk);
  1045. soc_camera_host_unregister(soc_host);
  1046. iounmap(mx3_cam->base);
  1047. /*
  1048. * The channel has either not been allocated,
  1049. * or should have been released
  1050. */
  1051. if (WARN_ON(mx3_cam->idmac_channel[0]))
  1052. dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
  1053. vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
  1054. vfree(mx3_cam);
  1055. dmaengine_put();
  1056. return 0;
  1057. }
  1058. static struct platform_driver mx3_camera_driver = {
  1059. .driver = {
  1060. .name = MX3_CAM_DRV_NAME,
  1061. },
  1062. .probe = mx3_camera_probe,
  1063. .remove = __devexit_p(mx3_camera_remove),
  1064. };
  1065. static int __init mx3_camera_init(void)
  1066. {
  1067. return platform_driver_register(&mx3_camera_driver);
  1068. }
  1069. static void __exit mx3_camera_exit(void)
  1070. {
  1071. platform_driver_unregister(&mx3_camera_driver);
  1072. }
  1073. module_init(mx3_camera_init);
  1074. module_exit(mx3_camera_exit);
  1075. MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
  1076. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1077. MODULE_LICENSE("GPL v2");
  1078. MODULE_VERSION("0.2.3");
  1079. MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);