mx3_camera.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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_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_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_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->v4l2_buf.timestamp);
  132. vb->v4l2_buf.field = mx3_cam->field;
  133. vb->v4l2_buf.sequence = mx3_cam->sequence++;
  134. vb2_buffer_done(vb, 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 struct v4l2_format *fmt,
  157. unsigned int *count, unsigned int *num_planes,
  158. unsigned int sizes[], void *alloc_ctxs[])
  159. {
  160. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  161. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  162. struct mx3_camera_dev *mx3_cam = ici->priv;
  163. if (!mx3_cam->idmac_channel[0])
  164. return -EINVAL;
  165. if (fmt) {
  166. const struct soc_camera_format_xlate *xlate = soc_camera_xlate_by_fourcc(icd,
  167. fmt->fmt.pix.pixelformat);
  168. unsigned int bytes_per_line;
  169. int ret;
  170. if (!xlate)
  171. return -EINVAL;
  172. ret = soc_mbus_bytes_per_line(fmt->fmt.pix.width,
  173. xlate->host_fmt);
  174. if (ret < 0)
  175. return ret;
  176. bytes_per_line = max_t(u32, fmt->fmt.pix.bytesperline, ret);
  177. ret = soc_mbus_image_size(xlate->host_fmt, bytes_per_line,
  178. fmt->fmt.pix.height);
  179. if (ret < 0)
  180. return ret;
  181. sizes[0] = max_t(u32, fmt->fmt.pix.sizeimage, ret);
  182. } else {
  183. /* Called from VIDIOC_REQBUFS or in compatibility mode */
  184. sizes[0] = icd->sizeimage;
  185. }
  186. alloc_ctxs[0] = mx3_cam->alloc_ctx;
  187. if (!vq->num_buffers)
  188. mx3_cam->sequence = 0;
  189. if (!*count)
  190. *count = 2;
  191. /* If *num_planes != 0, we have already verified *count. */
  192. if (!*num_planes &&
  193. sizes[0] * *count + mx3_cam->buf_total > MAX_VIDEO_MEM * 1024 * 1024)
  194. *count = (MAX_VIDEO_MEM * 1024 * 1024 - mx3_cam->buf_total) /
  195. sizes[0];
  196. *num_planes = 1;
  197. return 0;
  198. }
  199. static enum pixel_fmt fourcc_to_ipu_pix(__u32 fourcc)
  200. {
  201. /* Add more formats as need arises and test possibilities appear... */
  202. switch (fourcc) {
  203. case V4L2_PIX_FMT_RGB24:
  204. return IPU_PIX_FMT_RGB24;
  205. case V4L2_PIX_FMT_UYVY:
  206. case V4L2_PIX_FMT_RGB565:
  207. default:
  208. return IPU_PIX_FMT_GENERIC;
  209. }
  210. }
  211. static void mx3_videobuf_queue(struct vb2_buffer *vb)
  212. {
  213. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  214. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  215. struct mx3_camera_dev *mx3_cam = ici->priv;
  216. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  217. struct scatterlist *sg = &buf->sg;
  218. struct dma_async_tx_descriptor *txd;
  219. struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
  220. struct idmac_video_param *video = &ichan->params.video;
  221. const struct soc_mbus_pixelfmt *host_fmt = icd->current_fmt->host_fmt;
  222. dma_cookie_t cookie;
  223. size_t new_size;
  224. new_size = icd->sizeimage;
  225. if (vb2_plane_size(vb, 0) < new_size) {
  226. dev_err(icd->parent, "Buffer #%d too small (%lu < %zu)\n",
  227. vb->v4l2_buf.index, vb2_plane_size(vb, 0), new_size);
  228. goto error;
  229. }
  230. if (!buf->txd) {
  231. sg_dma_address(sg) = vb2_dma_contig_plane_dma_addr(vb, 0);
  232. sg_dma_len(sg) = new_size;
  233. txd = dmaengine_prep_slave_sg(
  234. &ichan->dma_chan, sg, 1, DMA_DEV_TO_MEM,
  235. DMA_PREP_INTERRUPT);
  236. if (!txd)
  237. goto error;
  238. txd->callback_param = txd;
  239. txd->callback = mx3_cam_dma_done;
  240. buf->txd = txd;
  241. } else {
  242. txd = buf->txd;
  243. }
  244. vb2_set_plane_payload(vb, 0, new_size);
  245. /* This is the configuration of one sg-element */
  246. video->out_pixel_fmt = fourcc_to_ipu_pix(host_fmt->fourcc);
  247. if (video->out_pixel_fmt == IPU_PIX_FMT_GENERIC) {
  248. /*
  249. * If the IPU DMA channel is configured to transfer generic
  250. * 8-bit data, we have to set up the geometry parameters
  251. * correctly, according to the current pixel format. The DMA
  252. * horizontal parameters in this case are expressed in bytes,
  253. * not in pixels.
  254. */
  255. video->out_width = icd->bytesperline;
  256. video->out_height = icd->user_height;
  257. video->out_stride = icd->bytesperline;
  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_irq(&mx3_cam->lock);
  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_irq(&mx3_cam->lock);
  288. error:
  289. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  290. }
  291. static void mx3_videobuf_release(struct vb2_buffer *vb)
  292. {
  293. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  294. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  295. struct mx3_camera_dev *mx3_cam = ici->priv;
  296. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  297. struct dma_async_tx_descriptor *txd = buf->txd;
  298. unsigned long flags;
  299. dev_dbg(icd->parent,
  300. "Release%s DMA 0x%08x, queue %sempty\n",
  301. mx3_cam->active == buf ? " active" : "", sg_dma_address(&buf->sg),
  302. list_empty(&buf->queue) ? "" : "not ");
  303. spin_lock_irqsave(&mx3_cam->lock, flags);
  304. if (mx3_cam->active == buf)
  305. mx3_cam->active = NULL;
  306. /* Doesn't hurt also if the list is empty */
  307. list_del_init(&buf->queue);
  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. mx3_cam->buf_total -= vb2_plane_size(vb, 0);
  315. }
  316. static int mx3_videobuf_init(struct vb2_buffer *vb)
  317. {
  318. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  319. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  320. struct mx3_camera_dev *mx3_cam = ici->priv;
  321. struct mx3_camera_buffer *buf = to_mx3_vb(vb);
  322. if (!buf->txd) {
  323. /* This is for locking debugging only */
  324. INIT_LIST_HEAD(&buf->queue);
  325. sg_init_table(&buf->sg, 1);
  326. mx3_cam->buf_total += vb2_plane_size(vb, 0);
  327. }
  328. return 0;
  329. }
  330. static void mx3_stop_streaming(struct vb2_queue *q)
  331. {
  332. struct soc_camera_device *icd = soc_camera_from_vb2q(q);
  333. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  334. struct mx3_camera_dev *mx3_cam = ici->priv;
  335. struct idmac_channel *ichan = mx3_cam->idmac_channel[0];
  336. struct mx3_camera_buffer *buf, *tmp;
  337. unsigned long flags;
  338. if (ichan)
  339. dmaengine_pause(&ichan->dma_chan);
  340. spin_lock_irqsave(&mx3_cam->lock, flags);
  341. mx3_cam->active = NULL;
  342. list_for_each_entry_safe(buf, tmp, &mx3_cam->capture, queue) {
  343. list_del_init(&buf->queue);
  344. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  345. }
  346. spin_unlock_irqrestore(&mx3_cam->lock, flags);
  347. }
  348. static struct vb2_ops mx3_videobuf_ops = {
  349. .queue_setup = mx3_videobuf_setup,
  350. .buf_queue = mx3_videobuf_queue,
  351. .buf_cleanup = mx3_videobuf_release,
  352. .buf_init = mx3_videobuf_init,
  353. .wait_prepare = vb2_ops_wait_prepare,
  354. .wait_finish = vb2_ops_wait_finish,
  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. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  361. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  362. q->io_modes = VB2_MMAP | VB2_USERPTR;
  363. q->drv_priv = icd;
  364. q->ops = &mx3_videobuf_ops;
  365. q->mem_ops = &vb2_dma_contig_memops;
  366. q->buf_struct_size = sizeof(struct mx3_camera_buffer);
  367. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  368. q->lock = &ici->host_lock;
  369. return vb2_queue_init(q);
  370. }
  371. /* First part of ipu_csi_init_interface() */
  372. static void mx3_camera_activate(struct mx3_camera_dev *mx3_cam)
  373. {
  374. u32 conf;
  375. long rate;
  376. /* Set default size: ipu_csi_set_window_size() */
  377. csi_reg_write(mx3_cam, (640 - 1) | ((480 - 1) << 16), CSI_ACT_FRM_SIZE);
  378. /* ...and position to 0:0: ipu_csi_set_window_pos() */
  379. conf = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
  380. csi_reg_write(mx3_cam, conf, CSI_OUT_FRM_CTRL);
  381. /* We use only gated clock synchronisation mode so far */
  382. conf = 0 << CSI_SENS_CONF_SENS_PRTCL_SHIFT;
  383. /* Set generic data, platform-biggest bus-width */
  384. conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
  385. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
  386. conf |= 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  387. else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
  388. conf |= 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  389. else if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
  390. conf |= 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  391. else/* if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)*/
  392. conf |= 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  393. if (mx3_cam->platform_flags & MX3_CAMERA_CLK_SRC)
  394. conf |= 1 << CSI_SENS_CONF_SENS_CLKSRC_SHIFT;
  395. if (mx3_cam->platform_flags & MX3_CAMERA_EXT_VSYNC)
  396. conf |= 1 << CSI_SENS_CONF_EXT_VSYNC_SHIFT;
  397. if (mx3_cam->platform_flags & MX3_CAMERA_DP)
  398. conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
  399. if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
  400. conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
  401. if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
  402. conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
  403. if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
  404. conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
  405. /* ipu_csi_init_interface() */
  406. csi_reg_write(mx3_cam, conf, CSI_SENS_CONF);
  407. clk_prepare_enable(mx3_cam->clk);
  408. rate = clk_round_rate(mx3_cam->clk, mx3_cam->mclk);
  409. dev_dbg(mx3_cam->soc_host.v4l2_dev.dev, "Set SENS_CONF to %x, rate %ld\n", conf, rate);
  410. if (rate)
  411. clk_set_rate(mx3_cam->clk, rate);
  412. }
  413. static int mx3_camera_add_device(struct soc_camera_device *icd)
  414. {
  415. dev_info(icd->parent, "MX3 Camera driver attached to camera %d\n",
  416. icd->devnum);
  417. return 0;
  418. }
  419. static void mx3_camera_remove_device(struct soc_camera_device *icd)
  420. {
  421. dev_info(icd->parent, "MX3 Camera driver detached from camera %d\n",
  422. icd->devnum);
  423. }
  424. /* Called with .host_lock held */
  425. static int mx3_camera_clock_start(struct soc_camera_host *ici)
  426. {
  427. struct mx3_camera_dev *mx3_cam = ici->priv;
  428. mx3_camera_activate(mx3_cam);
  429. mx3_cam->buf_total = 0;
  430. return 0;
  431. }
  432. /* Called with .host_lock held */
  433. static void mx3_camera_clock_stop(struct soc_camera_host *ici)
  434. {
  435. struct mx3_camera_dev *mx3_cam = ici->priv;
  436. struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
  437. if (*ichan) {
  438. dma_release_channel(&(*ichan)->dma_chan);
  439. *ichan = NULL;
  440. }
  441. clk_disable_unprepare(mx3_cam->clk);
  442. }
  443. static int test_platform_param(struct mx3_camera_dev *mx3_cam,
  444. unsigned char buswidth, unsigned long *flags)
  445. {
  446. /*
  447. * If requested data width is supported by the platform, use it or any
  448. * possible lower value - i.MX31 is smart enough to shift bits
  449. */
  450. if (buswidth > fls(mx3_cam->width_flags))
  451. return -EINVAL;
  452. /*
  453. * Platform specified synchronization and pixel clock polarities are
  454. * only a recommendation and are only used during probing. MX3x
  455. * camera interface only works in master mode, i.e., uses HSYNC and
  456. * VSYNC signals from the sensor
  457. */
  458. *flags = V4L2_MBUS_MASTER |
  459. V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  460. V4L2_MBUS_HSYNC_ACTIVE_LOW |
  461. V4L2_MBUS_VSYNC_ACTIVE_HIGH |
  462. V4L2_MBUS_VSYNC_ACTIVE_LOW |
  463. V4L2_MBUS_PCLK_SAMPLE_RISING |
  464. V4L2_MBUS_PCLK_SAMPLE_FALLING |
  465. V4L2_MBUS_DATA_ACTIVE_HIGH |
  466. V4L2_MBUS_DATA_ACTIVE_LOW;
  467. return 0;
  468. }
  469. static int mx3_camera_try_bus_param(struct soc_camera_device *icd,
  470. const unsigned int depth)
  471. {
  472. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  473. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  474. struct mx3_camera_dev *mx3_cam = ici->priv;
  475. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  476. unsigned long bus_flags, common_flags;
  477. int ret = test_platform_param(mx3_cam, depth, &bus_flags);
  478. dev_dbg(icd->parent, "request bus width %d bit: %d\n", depth, ret);
  479. if (ret < 0)
  480. return ret;
  481. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  482. if (!ret) {
  483. common_flags = soc_mbus_config_compatible(&cfg,
  484. bus_flags);
  485. if (!common_flags) {
  486. dev_warn(icd->parent,
  487. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  488. cfg.flags, bus_flags);
  489. return -EINVAL;
  490. }
  491. } else if (ret != -ENOIOCTLCMD) {
  492. return ret;
  493. }
  494. return 0;
  495. }
  496. static bool chan_filter(struct dma_chan *chan, void *arg)
  497. {
  498. struct dma_chan_request *rq = arg;
  499. struct mx3_camera_pdata *pdata;
  500. if (!imx_dma_is_ipu(chan))
  501. return false;
  502. if (!rq)
  503. return false;
  504. pdata = rq->mx3_cam->soc_host.v4l2_dev.dev->platform_data;
  505. return rq->id == chan->chan_id &&
  506. pdata->dma_dev == chan->device->dev;
  507. }
  508. static const struct soc_mbus_pixelfmt mx3_camera_formats[] = {
  509. {
  510. .fourcc = V4L2_PIX_FMT_SBGGR8,
  511. .name = "Bayer BGGR (sRGB) 8 bit",
  512. .bits_per_sample = 8,
  513. .packing = SOC_MBUS_PACKING_NONE,
  514. .order = SOC_MBUS_ORDER_LE,
  515. .layout = SOC_MBUS_LAYOUT_PACKED,
  516. }, {
  517. .fourcc = V4L2_PIX_FMT_GREY,
  518. .name = "Monochrome 8 bit",
  519. .bits_per_sample = 8,
  520. .packing = SOC_MBUS_PACKING_NONE,
  521. .order = SOC_MBUS_ORDER_LE,
  522. .layout = SOC_MBUS_LAYOUT_PACKED,
  523. },
  524. };
  525. /* This will be corrected as we get more formats */
  526. static bool mx3_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  527. {
  528. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  529. (fmt->bits_per_sample == 8 &&
  530. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  531. (fmt->bits_per_sample > 8 &&
  532. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  533. }
  534. static int mx3_camera_get_formats(struct soc_camera_device *icd, unsigned int idx,
  535. struct soc_camera_format_xlate *xlate)
  536. {
  537. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  538. struct device *dev = icd->parent;
  539. int formats = 0, ret;
  540. struct v4l2_subdev_mbus_code_enum code = {
  541. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  542. .index = idx,
  543. };
  544. const struct soc_mbus_pixelfmt *fmt;
  545. ret = v4l2_subdev_call(sd, pad, enum_mbus_code, NULL, &code);
  546. if (ret < 0)
  547. /* No more formats */
  548. return 0;
  549. fmt = soc_mbus_get_fmtdesc(code.code);
  550. if (!fmt) {
  551. dev_warn(icd->parent,
  552. "Unsupported format code #%u: 0x%x\n", idx, code.code);
  553. return 0;
  554. }
  555. /* This also checks support for the requested bits-per-sample */
  556. ret = mx3_camera_try_bus_param(icd, fmt->bits_per_sample);
  557. if (ret < 0)
  558. return 0;
  559. switch (code.code) {
  560. case MEDIA_BUS_FMT_SBGGR10_1X10:
  561. formats++;
  562. if (xlate) {
  563. xlate->host_fmt = &mx3_camera_formats[0];
  564. xlate->code = code.code;
  565. xlate++;
  566. dev_dbg(dev, "Providing format %s using code 0x%x\n",
  567. mx3_camera_formats[0].name, code.code);
  568. }
  569. break;
  570. case MEDIA_BUS_FMT_Y10_1X10:
  571. formats++;
  572. if (xlate) {
  573. xlate->host_fmt = &mx3_camera_formats[1];
  574. xlate->code = code.code;
  575. xlate++;
  576. dev_dbg(dev, "Providing format %s using code 0x%x\n",
  577. mx3_camera_formats[1].name, code.code);
  578. }
  579. break;
  580. default:
  581. if (!mx3_camera_packing_supported(fmt))
  582. return 0;
  583. }
  584. /* Generic pass-through */
  585. formats++;
  586. if (xlate) {
  587. xlate->host_fmt = fmt;
  588. xlate->code = code.code;
  589. dev_dbg(dev, "Providing format %c%c%c%c in pass-through mode\n",
  590. (fmt->fourcc >> (0*8)) & 0xFF,
  591. (fmt->fourcc >> (1*8)) & 0xFF,
  592. (fmt->fourcc >> (2*8)) & 0xFF,
  593. (fmt->fourcc >> (3*8)) & 0xFF);
  594. xlate++;
  595. }
  596. return formats;
  597. }
  598. static void configure_geometry(struct mx3_camera_dev *mx3_cam,
  599. unsigned int width, unsigned int height,
  600. const struct soc_mbus_pixelfmt *fmt)
  601. {
  602. u32 ctrl, width_field, height_field;
  603. if (fourcc_to_ipu_pix(fmt->fourcc) == IPU_PIX_FMT_GENERIC) {
  604. /*
  605. * As the CSI will be configured to output BAYER, here
  606. * the width parameter count the number of samples to
  607. * capture to complete the whole image width.
  608. */
  609. unsigned int num, den;
  610. int ret = soc_mbus_samples_per_pixel(fmt, &num, &den);
  611. BUG_ON(ret < 0);
  612. width = width * num / den;
  613. }
  614. /* Setup frame size - this cannot be changed on-the-fly... */
  615. width_field = width - 1;
  616. height_field = height - 1;
  617. csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_SENS_FRM_SIZE);
  618. csi_reg_write(mx3_cam, width_field << 16, CSI_FLASH_STROBE_1);
  619. csi_reg_write(mx3_cam, (height_field << 16) | 0x22, CSI_FLASH_STROBE_2);
  620. csi_reg_write(mx3_cam, width_field | (height_field << 16), CSI_ACT_FRM_SIZE);
  621. /* ...and position */
  622. ctrl = csi_reg_read(mx3_cam, CSI_OUT_FRM_CTRL) & 0xffff0000;
  623. /* Sensor does the cropping */
  624. csi_reg_write(mx3_cam, ctrl | 0 | (0 << 8), CSI_OUT_FRM_CTRL);
  625. }
  626. static int acquire_dma_channel(struct mx3_camera_dev *mx3_cam)
  627. {
  628. dma_cap_mask_t mask;
  629. struct dma_chan *chan;
  630. struct idmac_channel **ichan = &mx3_cam->idmac_channel[0];
  631. /* We have to use IDMAC_IC_7 for Bayer / generic data */
  632. struct dma_chan_request rq = {.mx3_cam = mx3_cam,
  633. .id = IDMAC_IC_7};
  634. dma_cap_zero(mask);
  635. dma_cap_set(DMA_SLAVE, mask);
  636. dma_cap_set(DMA_PRIVATE, mask);
  637. chan = dma_request_channel(mask, chan_filter, &rq);
  638. if (!chan)
  639. return -EBUSY;
  640. *ichan = to_idmac_chan(chan);
  641. (*ichan)->client = mx3_cam;
  642. return 0;
  643. }
  644. /*
  645. * FIXME: learn to use stride != width, then we can keep stride properly aligned
  646. * and support arbitrary (even) widths.
  647. */
  648. static inline void stride_align(__u32 *width)
  649. {
  650. if (ALIGN(*width, 8) < 4096)
  651. *width = ALIGN(*width, 8);
  652. else
  653. *width = *width & ~7;
  654. }
  655. /*
  656. * As long as we don't implement host-side cropping and scaling, we can use
  657. * default g_crop and cropcap from soc_camera.c
  658. */
  659. static int mx3_camera_set_crop(struct soc_camera_device *icd,
  660. const struct v4l2_crop *a)
  661. {
  662. struct v4l2_crop a_writable = *a;
  663. struct v4l2_rect *rect = &a_writable.c;
  664. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  665. struct mx3_camera_dev *mx3_cam = ici->priv;
  666. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  667. struct v4l2_subdev_format fmt = {
  668. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  669. };
  670. struct v4l2_mbus_framefmt *mf = &fmt.format;
  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, pad, get_fmt, NULL, &fmt);
  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, pad, set_fmt, NULL, &fmt);
  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_subdev_format format = {
  708. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  709. };
  710. struct v4l2_mbus_framefmt *mf = &format.format;
  711. int ret;
  712. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  713. if (!xlate) {
  714. dev_warn(icd->parent, "Format %x not found\n",
  715. pix->pixelformat);
  716. return -EINVAL;
  717. }
  718. stride_align(&pix->width);
  719. dev_dbg(icd->parent, "Set format %dx%d\n", pix->width, pix->height);
  720. /*
  721. * Might have to perform a complete interface initialisation like in
  722. * ipu_csi_init_interface() in mxc_v4l2_s_param(). Also consider
  723. * mxc_v4l2_s_fmt()
  724. */
  725. configure_geometry(mx3_cam, pix->width, pix->height, xlate->host_fmt);
  726. mf->width = pix->width;
  727. mf->height = pix->height;
  728. mf->field = pix->field;
  729. mf->colorspace = pix->colorspace;
  730. mf->code = xlate->code;
  731. ret = v4l2_subdev_call(sd, pad, set_fmt, NULL, &format);
  732. if (ret < 0)
  733. return ret;
  734. if (mf->code != xlate->code)
  735. return -EINVAL;
  736. if (!mx3_cam->idmac_channel[0]) {
  737. ret = acquire_dma_channel(mx3_cam);
  738. if (ret < 0)
  739. return ret;
  740. }
  741. pix->width = mf->width;
  742. pix->height = mf->height;
  743. pix->field = mf->field;
  744. mx3_cam->field = mf->field;
  745. pix->colorspace = mf->colorspace;
  746. icd->current_fmt = xlate;
  747. dev_dbg(icd->parent, "Sensor set %dx%d\n", pix->width, pix->height);
  748. return ret;
  749. }
  750. static int mx3_camera_try_fmt(struct soc_camera_device *icd,
  751. struct v4l2_format *f)
  752. {
  753. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  754. const struct soc_camera_format_xlate *xlate;
  755. struct v4l2_pix_format *pix = &f->fmt.pix;
  756. struct v4l2_subdev_pad_config pad_cfg;
  757. struct v4l2_subdev_format format = {
  758. .which = V4L2_SUBDEV_FORMAT_TRY,
  759. };
  760. struct v4l2_mbus_framefmt *mf = &format.format;
  761. __u32 pixfmt = pix->pixelformat;
  762. int ret;
  763. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  764. if (pixfmt && !xlate) {
  765. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  766. return -EINVAL;
  767. }
  768. /* limit to MX3 hardware capabilities */
  769. if (pix->height > 4096)
  770. pix->height = 4096;
  771. if (pix->width > 4096)
  772. pix->width = 4096;
  773. /* limit to sensor capabilities */
  774. mf->width = pix->width;
  775. mf->height = pix->height;
  776. mf->field = pix->field;
  777. mf->colorspace = pix->colorspace;
  778. mf->code = xlate->code;
  779. ret = v4l2_subdev_call(sd, pad, set_fmt, &pad_cfg, &format);
  780. if (ret < 0)
  781. return ret;
  782. pix->width = mf->width;
  783. pix->height = mf->height;
  784. pix->colorspace = mf->colorspace;
  785. switch (mf->field) {
  786. case V4L2_FIELD_ANY:
  787. pix->field = V4L2_FIELD_NONE;
  788. break;
  789. case V4L2_FIELD_NONE:
  790. break;
  791. default:
  792. dev_err(icd->parent, "Field type %d unsupported.\n",
  793. mf->field);
  794. ret = -EINVAL;
  795. }
  796. return ret;
  797. }
  798. static int mx3_camera_reqbufs(struct soc_camera_device *icd,
  799. struct v4l2_requestbuffers *p)
  800. {
  801. return 0;
  802. }
  803. static unsigned int mx3_camera_poll(struct file *file, poll_table *pt)
  804. {
  805. struct soc_camera_device *icd = file->private_data;
  806. return vb2_poll(&icd->vb2_vidq, file, pt);
  807. }
  808. static int mx3_camera_querycap(struct soc_camera_host *ici,
  809. struct v4l2_capability *cap)
  810. {
  811. /* cap->name is set by the firendly caller:-> */
  812. strlcpy(cap->card, "i.MX3x Camera", sizeof(cap->card));
  813. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  814. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  815. return 0;
  816. }
  817. static int mx3_camera_set_bus_param(struct soc_camera_device *icd)
  818. {
  819. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  820. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  821. struct mx3_camera_dev *mx3_cam = ici->priv;
  822. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  823. u32 pixfmt = icd->current_fmt->host_fmt->fourcc;
  824. unsigned long bus_flags, common_flags;
  825. u32 dw, sens_conf;
  826. const struct soc_mbus_pixelfmt *fmt;
  827. int buswidth;
  828. int ret;
  829. const struct soc_camera_format_xlate *xlate;
  830. struct device *dev = icd->parent;
  831. fmt = soc_mbus_get_fmtdesc(icd->current_fmt->code);
  832. if (!fmt)
  833. return -EINVAL;
  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. buswidth = fmt->bits_per_sample;
  840. ret = test_platform_param(mx3_cam, buswidth, &bus_flags);
  841. dev_dbg(dev, "requested bus width %d bit: %d\n", buswidth, ret);
  842. if (ret < 0)
  843. return ret;
  844. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  845. if (!ret) {
  846. common_flags = soc_mbus_config_compatible(&cfg,
  847. bus_flags);
  848. if (!common_flags) {
  849. dev_warn(icd->parent,
  850. "Flags incompatible: camera 0x%x, host 0x%lx\n",
  851. cfg.flags, bus_flags);
  852. return -EINVAL;
  853. }
  854. } else if (ret != -ENOIOCTLCMD) {
  855. return ret;
  856. } else {
  857. common_flags = bus_flags;
  858. }
  859. dev_dbg(dev, "Flags cam: 0x%x host: 0x%lx common: 0x%lx\n",
  860. cfg.flags, bus_flags, common_flags);
  861. /* Make choices, based on platform preferences */
  862. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  863. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  864. if (mx3_cam->platform_flags & MX3_CAMERA_HSP)
  865. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  866. else
  867. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  868. }
  869. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  870. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  871. if (mx3_cam->platform_flags & MX3_CAMERA_VSP)
  872. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  873. else
  874. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  875. }
  876. if ((common_flags & V4L2_MBUS_DATA_ACTIVE_HIGH) &&
  877. (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)) {
  878. if (mx3_cam->platform_flags & MX3_CAMERA_DP)
  879. common_flags &= ~V4L2_MBUS_DATA_ACTIVE_HIGH;
  880. else
  881. common_flags &= ~V4L2_MBUS_DATA_ACTIVE_LOW;
  882. }
  883. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  884. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  885. if (mx3_cam->platform_flags & MX3_CAMERA_PCP)
  886. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  887. else
  888. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  889. }
  890. cfg.flags = common_flags;
  891. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  892. if (ret < 0 && ret != -ENOIOCTLCMD) {
  893. dev_dbg(dev, "camera s_mbus_config(0x%lx) returned %d\n",
  894. common_flags, ret);
  895. return ret;
  896. }
  897. /*
  898. * So far only gated clock mode is supported. Add a line
  899. * (3 << CSI_SENS_CONF_SENS_PRTCL_SHIFT) |
  900. * below and select the required mode when supporting other
  901. * synchronisation protocols.
  902. */
  903. sens_conf = csi_reg_read(mx3_cam, CSI_SENS_CONF) &
  904. ~((1 << CSI_SENS_CONF_VSYNC_POL_SHIFT) |
  905. (1 << CSI_SENS_CONF_HSYNC_POL_SHIFT) |
  906. (1 << CSI_SENS_CONF_DATA_POL_SHIFT) |
  907. (1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT) |
  908. (3 << CSI_SENS_CONF_DATA_FMT_SHIFT) |
  909. (3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT));
  910. /* TODO: Support RGB and YUV formats */
  911. /* This has been set in mx3_camera_activate(), but we clear it above */
  912. sens_conf |= CSI_SENS_CONF_DATA_FMT_BAYER;
  913. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  914. sens_conf |= 1 << CSI_SENS_CONF_PIX_CLK_POL_SHIFT;
  915. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  916. sens_conf |= 1 << CSI_SENS_CONF_HSYNC_POL_SHIFT;
  917. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  918. sens_conf |= 1 << CSI_SENS_CONF_VSYNC_POL_SHIFT;
  919. if (common_flags & V4L2_MBUS_DATA_ACTIVE_LOW)
  920. sens_conf |= 1 << CSI_SENS_CONF_DATA_POL_SHIFT;
  921. /* Just do what we're asked to do */
  922. switch (xlate->host_fmt->bits_per_sample) {
  923. case 4:
  924. dw = 0 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  925. break;
  926. case 8:
  927. dw = 1 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  928. break;
  929. case 10:
  930. dw = 2 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  931. break;
  932. default:
  933. /*
  934. * Actually it can only be 15 now, default is just to silence
  935. * compiler warnings
  936. */
  937. case 15:
  938. dw = 3 << CSI_SENS_CONF_DATA_WIDTH_SHIFT;
  939. }
  940. csi_reg_write(mx3_cam, sens_conf | dw, CSI_SENS_CONF);
  941. dev_dbg(dev, "Set SENS_CONF to %x\n", sens_conf | dw);
  942. return 0;
  943. }
  944. static struct soc_camera_host_ops mx3_soc_camera_host_ops = {
  945. .owner = THIS_MODULE,
  946. .add = mx3_camera_add_device,
  947. .remove = mx3_camera_remove_device,
  948. .clock_start = mx3_camera_clock_start,
  949. .clock_stop = mx3_camera_clock_stop,
  950. .set_crop = mx3_camera_set_crop,
  951. .set_fmt = mx3_camera_set_fmt,
  952. .try_fmt = mx3_camera_try_fmt,
  953. .get_formats = mx3_camera_get_formats,
  954. .init_videobuf2 = mx3_camera_init_videobuf,
  955. .reqbufs = mx3_camera_reqbufs,
  956. .poll = mx3_camera_poll,
  957. .querycap = mx3_camera_querycap,
  958. .set_bus_param = mx3_camera_set_bus_param,
  959. };
  960. static int mx3_camera_probe(struct platform_device *pdev)
  961. {
  962. struct mx3_camera_pdata *pdata = pdev->dev.platform_data;
  963. struct mx3_camera_dev *mx3_cam;
  964. struct resource *res;
  965. void __iomem *base;
  966. int err = 0;
  967. struct soc_camera_host *soc_host;
  968. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  969. base = devm_ioremap_resource(&pdev->dev, res);
  970. if (IS_ERR(base))
  971. return PTR_ERR(base);
  972. if (!pdata)
  973. return -EINVAL;
  974. mx3_cam = devm_kzalloc(&pdev->dev, sizeof(*mx3_cam), GFP_KERNEL);
  975. if (!mx3_cam) {
  976. dev_err(&pdev->dev, "Could not allocate mx3 camera object\n");
  977. return -ENOMEM;
  978. }
  979. mx3_cam->clk = devm_clk_get(&pdev->dev, NULL);
  980. if (IS_ERR(mx3_cam->clk))
  981. return PTR_ERR(mx3_cam->clk);
  982. mx3_cam->pdata = pdata;
  983. mx3_cam->platform_flags = pdata->flags;
  984. if (!(mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_MASK)) {
  985. /*
  986. * Platform hasn't set available data widths. This is bad.
  987. * Warn and use a default.
  988. */
  989. dev_warn(&pdev->dev, "WARNING! Platform hasn't set available "
  990. "data widths, using default 8 bit\n");
  991. mx3_cam->platform_flags |= MX3_CAMERA_DATAWIDTH_8;
  992. }
  993. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_4)
  994. mx3_cam->width_flags = 1 << 3;
  995. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_8)
  996. mx3_cam->width_flags |= 1 << 7;
  997. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_10)
  998. mx3_cam->width_flags |= 1 << 9;
  999. if (mx3_cam->platform_flags & MX3_CAMERA_DATAWIDTH_15)
  1000. mx3_cam->width_flags |= 1 << 14;
  1001. mx3_cam->mclk = pdata->mclk_10khz * 10000;
  1002. if (!mx3_cam->mclk) {
  1003. dev_warn(&pdev->dev,
  1004. "mclk_10khz == 0! Please, fix your platform data. "
  1005. "Using default 20MHz\n");
  1006. mx3_cam->mclk = 20000000;
  1007. }
  1008. /* list of video-buffers */
  1009. INIT_LIST_HEAD(&mx3_cam->capture);
  1010. spin_lock_init(&mx3_cam->lock);
  1011. mx3_cam->base = base;
  1012. soc_host = &mx3_cam->soc_host;
  1013. soc_host->drv_name = MX3_CAM_DRV_NAME;
  1014. soc_host->ops = &mx3_soc_camera_host_ops;
  1015. soc_host->priv = mx3_cam;
  1016. soc_host->v4l2_dev.dev = &pdev->dev;
  1017. soc_host->nr = pdev->id;
  1018. mx3_cam->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  1019. if (IS_ERR(mx3_cam->alloc_ctx))
  1020. return PTR_ERR(mx3_cam->alloc_ctx);
  1021. if (pdata->asd_sizes) {
  1022. soc_host->asd = pdata->asd;
  1023. soc_host->asd_sizes = pdata->asd_sizes;
  1024. }
  1025. err = soc_camera_host_register(soc_host);
  1026. if (err)
  1027. goto ecamhostreg;
  1028. /* IDMAC interface */
  1029. dmaengine_get();
  1030. return 0;
  1031. ecamhostreg:
  1032. vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
  1033. return err;
  1034. }
  1035. static int mx3_camera_remove(struct platform_device *pdev)
  1036. {
  1037. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1038. struct mx3_camera_dev *mx3_cam = container_of(soc_host,
  1039. struct mx3_camera_dev, soc_host);
  1040. soc_camera_host_unregister(soc_host);
  1041. /*
  1042. * The channel has either not been allocated,
  1043. * or should have been released
  1044. */
  1045. if (WARN_ON(mx3_cam->idmac_channel[0]))
  1046. dma_release_channel(&mx3_cam->idmac_channel[0]->dma_chan);
  1047. vb2_dma_contig_cleanup_ctx(mx3_cam->alloc_ctx);
  1048. dmaengine_put();
  1049. return 0;
  1050. }
  1051. static struct platform_driver mx3_camera_driver = {
  1052. .driver = {
  1053. .name = MX3_CAM_DRV_NAME,
  1054. },
  1055. .probe = mx3_camera_probe,
  1056. .remove = mx3_camera_remove,
  1057. };
  1058. module_platform_driver(mx3_camera_driver);
  1059. MODULE_DESCRIPTION("i.MX3x SoC Camera Host driver");
  1060. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1061. MODULE_LICENSE("GPL v2");
  1062. MODULE_VERSION("0.2.3");
  1063. MODULE_ALIAS("platform:" MX3_CAM_DRV_NAME);