mx3_camera.c 35 KB

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