mx3_camera.c 35 KB

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