mx3_camera.c 35 KB

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