mx3_camera.c 35 KB

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