mx3_camera.c 34 KB

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