mx3_camera.c 35 KB

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