mx3_camera.c 34 KB

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