mx3_camera.c 33 KB

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