atmel-isi.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * Copyright (c) 2011 Atmel Corporation
  3. * Josh Wu, <josh.wu@atmel.com>
  4. *
  5. * Based on previous work by Lars Haring, <lars.haring@atmel.com>
  6. * and Sedji Gaouaou
  7. * Based on the bttv driver for Bt848 with respective copyright holders
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/fs.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <media/atmel-isi.h>
  24. #include <media/soc_camera.h>
  25. #include <media/soc_mediabus.h>
  26. #include <media/videobuf2-dma-contig.h>
  27. #define MAX_BUFFER_NUM 32
  28. #define MAX_SUPPORT_WIDTH 2048
  29. #define MAX_SUPPORT_HEIGHT 2048
  30. #define VID_LIMIT_BYTES (16 * 1024 * 1024)
  31. #define MIN_FRAME_RATE 15
  32. #define FRAME_INTERVAL_MILLI_SEC (1000 / MIN_FRAME_RATE)
  33. /* ISI states */
  34. enum {
  35. ISI_STATE_IDLE = 0,
  36. ISI_STATE_READY,
  37. ISI_STATE_WAIT_SOF,
  38. };
  39. /* Frame buffer descriptor */
  40. struct fbd {
  41. /* Physical address of the frame buffer */
  42. u32 fb_address;
  43. /* DMA Control Register(only in HISI2) */
  44. u32 dma_ctrl;
  45. /* Physical address of the next fbd */
  46. u32 next_fbd_address;
  47. };
  48. static void set_dma_ctrl(struct fbd *fb_desc, u32 ctrl)
  49. {
  50. fb_desc->dma_ctrl = ctrl;
  51. }
  52. struct isi_dma_desc {
  53. struct list_head list;
  54. struct fbd *p_fbd;
  55. u32 fbd_phys;
  56. };
  57. /* Frame buffer data */
  58. struct frame_buffer {
  59. struct vb2_buffer vb;
  60. struct isi_dma_desc *p_dma_desc;
  61. struct list_head list;
  62. };
  63. struct atmel_isi {
  64. /* Protects the access of variables shared with the ISR */
  65. spinlock_t lock;
  66. void __iomem *regs;
  67. int sequence;
  68. /* State of the ISI module in capturing mode */
  69. int state;
  70. /* Wait queue for waiting for SOF */
  71. wait_queue_head_t vsync_wq;
  72. struct vb2_alloc_ctx *alloc_ctx;
  73. /* Allocate descriptors for dma buffer use */
  74. struct fbd *p_fb_descriptors;
  75. u32 fb_descriptors_phys;
  76. struct list_head dma_desc_head;
  77. struct isi_dma_desc dma_desc[MAX_BUFFER_NUM];
  78. struct completion complete;
  79. struct clk *pclk;
  80. unsigned int irq;
  81. struct isi_platform_data *pdata;
  82. struct list_head video_buffer_list;
  83. struct frame_buffer *active;
  84. struct soc_camera_device *icd;
  85. struct soc_camera_host soc_host;
  86. };
  87. static void isi_writel(struct atmel_isi *isi, u32 reg, u32 val)
  88. {
  89. writel(val, isi->regs + reg);
  90. }
  91. static u32 isi_readl(struct atmel_isi *isi, u32 reg)
  92. {
  93. return readl(isi->regs + reg);
  94. }
  95. static int configure_geometry(struct atmel_isi *isi, u32 width,
  96. u32 height, enum v4l2_mbus_pixelcode code)
  97. {
  98. u32 cfg2, cr;
  99. switch (code) {
  100. /* YUV, including grey */
  101. case V4L2_MBUS_FMT_Y8_1X8:
  102. cr = ISI_CFG2_GRAYSCALE;
  103. break;
  104. case V4L2_MBUS_FMT_UYVY8_2X8:
  105. cr = ISI_CFG2_YCC_SWAP_MODE_3;
  106. break;
  107. case V4L2_MBUS_FMT_VYUY8_2X8:
  108. cr = ISI_CFG2_YCC_SWAP_MODE_2;
  109. break;
  110. case V4L2_MBUS_FMT_YUYV8_2X8:
  111. cr = ISI_CFG2_YCC_SWAP_MODE_1;
  112. break;
  113. case V4L2_MBUS_FMT_YVYU8_2X8:
  114. cr = ISI_CFG2_YCC_SWAP_DEFAULT;
  115. break;
  116. /* RGB, TODO */
  117. default:
  118. return -EINVAL;
  119. }
  120. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  121. cfg2 = isi_readl(isi, ISI_CFG2);
  122. cfg2 |= cr;
  123. /* Set width */
  124. cfg2 &= ~(ISI_CFG2_IM_HSIZE_MASK);
  125. cfg2 |= ((width - 1) << ISI_CFG2_IM_HSIZE_OFFSET) &
  126. ISI_CFG2_IM_HSIZE_MASK;
  127. /* Set height */
  128. cfg2 &= ~(ISI_CFG2_IM_VSIZE_MASK);
  129. cfg2 |= ((height - 1) << ISI_CFG2_IM_VSIZE_OFFSET)
  130. & ISI_CFG2_IM_VSIZE_MASK;
  131. isi_writel(isi, ISI_CFG2, cfg2);
  132. return 0;
  133. }
  134. static irqreturn_t atmel_isi_handle_streaming(struct atmel_isi *isi)
  135. {
  136. if (isi->active) {
  137. struct vb2_buffer *vb = &isi->active->vb;
  138. struct frame_buffer *buf = isi->active;
  139. list_del_init(&buf->list);
  140. do_gettimeofday(&vb->v4l2_buf.timestamp);
  141. vb->v4l2_buf.sequence = isi->sequence++;
  142. vb2_buffer_done(vb, VB2_BUF_STATE_DONE);
  143. }
  144. if (list_empty(&isi->video_buffer_list)) {
  145. isi->active = NULL;
  146. } else {
  147. /* start next dma frame. */
  148. isi->active = list_entry(isi->video_buffer_list.next,
  149. struct frame_buffer, list);
  150. isi_writel(isi, ISI_DMA_C_DSCR,
  151. isi->active->p_dma_desc->fbd_phys);
  152. isi_writel(isi, ISI_DMA_C_CTRL,
  153. ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
  154. isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
  155. }
  156. return IRQ_HANDLED;
  157. }
  158. /* ISI interrupt service routine */
  159. static irqreturn_t isi_interrupt(int irq, void *dev_id)
  160. {
  161. struct atmel_isi *isi = dev_id;
  162. u32 status, mask, pending;
  163. irqreturn_t ret = IRQ_NONE;
  164. spin_lock(&isi->lock);
  165. status = isi_readl(isi, ISI_STATUS);
  166. mask = isi_readl(isi, ISI_INTMASK);
  167. pending = status & mask;
  168. if (pending & ISI_CTRL_SRST) {
  169. complete(&isi->complete);
  170. isi_writel(isi, ISI_INTDIS, ISI_CTRL_SRST);
  171. ret = IRQ_HANDLED;
  172. } else if (pending & ISI_CTRL_DIS) {
  173. complete(&isi->complete);
  174. isi_writel(isi, ISI_INTDIS, ISI_CTRL_DIS);
  175. ret = IRQ_HANDLED;
  176. } else {
  177. if ((pending & ISI_SR_VSYNC) &&
  178. (isi->state == ISI_STATE_IDLE)) {
  179. isi->state = ISI_STATE_READY;
  180. wake_up_interruptible(&isi->vsync_wq);
  181. ret = IRQ_HANDLED;
  182. }
  183. if (likely(pending & ISI_SR_CXFR_DONE))
  184. ret = atmel_isi_handle_streaming(isi);
  185. }
  186. spin_unlock(&isi->lock);
  187. return ret;
  188. }
  189. #define WAIT_ISI_RESET 1
  190. #define WAIT_ISI_DISABLE 0
  191. static int atmel_isi_wait_status(struct atmel_isi *isi, int wait_reset)
  192. {
  193. unsigned long timeout;
  194. /*
  195. * The reset or disable will only succeed if we have a
  196. * pixel clock from the camera.
  197. */
  198. init_completion(&isi->complete);
  199. if (wait_reset) {
  200. isi_writel(isi, ISI_INTEN, ISI_CTRL_SRST);
  201. isi_writel(isi, ISI_CTRL, ISI_CTRL_SRST);
  202. } else {
  203. isi_writel(isi, ISI_INTEN, ISI_CTRL_DIS);
  204. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  205. }
  206. timeout = wait_for_completion_timeout(&isi->complete,
  207. msecs_to_jiffies(100));
  208. if (timeout == 0)
  209. return -ETIMEDOUT;
  210. return 0;
  211. }
  212. /* ------------------------------------------------------------------
  213. Videobuf operations
  214. ------------------------------------------------------------------*/
  215. static int queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
  216. unsigned int *nplanes, unsigned long sizes[],
  217. void *alloc_ctxs[])
  218. {
  219. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  220. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  221. struct atmel_isi *isi = ici->priv;
  222. unsigned long size;
  223. int ret, bytes_per_line;
  224. /* Reset ISI */
  225. ret = atmel_isi_wait_status(isi, WAIT_ISI_RESET);
  226. if (ret < 0) {
  227. dev_err(icd->parent, "Reset ISI timed out\n");
  228. return ret;
  229. }
  230. /* Disable all interrupts */
  231. isi_writel(isi, ISI_INTDIS, ~0UL);
  232. bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  233. icd->current_fmt->host_fmt);
  234. if (bytes_per_line < 0)
  235. return bytes_per_line;
  236. size = bytes_per_line * icd->user_height;
  237. if (!*nbuffers || *nbuffers > MAX_BUFFER_NUM)
  238. *nbuffers = MAX_BUFFER_NUM;
  239. if (size * *nbuffers > VID_LIMIT_BYTES)
  240. *nbuffers = VID_LIMIT_BYTES / size;
  241. *nplanes = 1;
  242. sizes[0] = size;
  243. alloc_ctxs[0] = isi->alloc_ctx;
  244. isi->sequence = 0;
  245. isi->active = NULL;
  246. dev_dbg(icd->parent, "%s, count=%d, size=%ld\n", __func__,
  247. *nbuffers, size);
  248. return 0;
  249. }
  250. static int buffer_init(struct vb2_buffer *vb)
  251. {
  252. struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
  253. buf->p_dma_desc = NULL;
  254. INIT_LIST_HEAD(&buf->list);
  255. return 0;
  256. }
  257. static int buffer_prepare(struct vb2_buffer *vb)
  258. {
  259. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  260. struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
  261. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  262. struct atmel_isi *isi = ici->priv;
  263. unsigned long size;
  264. struct isi_dma_desc *desc;
  265. int bytes_per_line = soc_mbus_bytes_per_line(icd->user_width,
  266. icd->current_fmt->host_fmt);
  267. if (bytes_per_line < 0)
  268. return bytes_per_line;
  269. size = bytes_per_line * icd->user_height;
  270. if (vb2_plane_size(vb, 0) < size) {
  271. dev_err(icd->parent, "%s data will not fit into plane (%lu < %lu)\n",
  272. __func__, vb2_plane_size(vb, 0), size);
  273. return -EINVAL;
  274. }
  275. vb2_set_plane_payload(&buf->vb, 0, size);
  276. if (!buf->p_dma_desc) {
  277. if (list_empty(&isi->dma_desc_head)) {
  278. dev_err(icd->parent, "Not enough dma descriptors.\n");
  279. return -EINVAL;
  280. } else {
  281. /* Get an available descriptor */
  282. desc = list_entry(isi->dma_desc_head.next,
  283. struct isi_dma_desc, list);
  284. /* Delete the descriptor since now it is used */
  285. list_del_init(&desc->list);
  286. /* Initialize the dma descriptor */
  287. desc->p_fbd->fb_address =
  288. vb2_dma_contig_plane_paddr(vb, 0);
  289. desc->p_fbd->next_fbd_address = 0;
  290. set_dma_ctrl(desc->p_fbd, ISI_DMA_CTRL_WB);
  291. buf->p_dma_desc = desc;
  292. }
  293. }
  294. return 0;
  295. }
  296. static void buffer_cleanup(struct vb2_buffer *vb)
  297. {
  298. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  299. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  300. struct atmel_isi *isi = ici->priv;
  301. struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
  302. /* This descriptor is available now and we add to head list */
  303. if (buf->p_dma_desc)
  304. list_add(&buf->p_dma_desc->list, &isi->dma_desc_head);
  305. }
  306. static void start_dma(struct atmel_isi *isi, struct frame_buffer *buffer)
  307. {
  308. u32 ctrl, cfg1;
  309. cfg1 = isi_readl(isi, ISI_CFG1);
  310. /* Enable irq: cxfr for the codec path, pxfr for the preview path */
  311. isi_writel(isi, ISI_INTEN,
  312. ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
  313. /* Check if already in a frame */
  314. if (isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) {
  315. dev_err(isi->icd->parent, "Already in frame handling.\n");
  316. return;
  317. }
  318. isi_writel(isi, ISI_DMA_C_DSCR, buffer->p_dma_desc->fbd_phys);
  319. isi_writel(isi, ISI_DMA_C_CTRL, ISI_DMA_CTRL_FETCH | ISI_DMA_CTRL_DONE);
  320. isi_writel(isi, ISI_DMA_CHER, ISI_DMA_CHSR_C_CH);
  321. /* Enable linked list */
  322. cfg1 |= isi->pdata->frate | ISI_CFG1_DISCR;
  323. /* Enable codec path and ISI */
  324. ctrl = ISI_CTRL_CDC | ISI_CTRL_EN;
  325. isi_writel(isi, ISI_CTRL, ctrl);
  326. isi_writel(isi, ISI_CFG1, cfg1);
  327. }
  328. static void buffer_queue(struct vb2_buffer *vb)
  329. {
  330. struct soc_camera_device *icd = soc_camera_from_vb2q(vb->vb2_queue);
  331. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  332. struct atmel_isi *isi = ici->priv;
  333. struct frame_buffer *buf = container_of(vb, struct frame_buffer, vb);
  334. unsigned long flags = 0;
  335. spin_lock_irqsave(&isi->lock, flags);
  336. list_add_tail(&buf->list, &isi->video_buffer_list);
  337. if (isi->active == NULL) {
  338. isi->active = buf;
  339. start_dma(isi, buf);
  340. }
  341. spin_unlock_irqrestore(&isi->lock, flags);
  342. }
  343. static int start_streaming(struct vb2_queue *vq)
  344. {
  345. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  346. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  347. struct atmel_isi *isi = ici->priv;
  348. u32 sr = 0;
  349. int ret;
  350. spin_lock_irq(&isi->lock);
  351. isi->state = ISI_STATE_IDLE;
  352. /* Clear any pending SOF interrupt */
  353. sr = isi_readl(isi, ISI_STATUS);
  354. /* Enable VSYNC interrupt for SOF */
  355. isi_writel(isi, ISI_INTEN, ISI_SR_VSYNC);
  356. isi_writel(isi, ISI_CTRL, ISI_CTRL_EN);
  357. spin_unlock_irq(&isi->lock);
  358. dev_dbg(icd->parent, "Waiting for SOF\n");
  359. ret = wait_event_interruptible(isi->vsync_wq,
  360. isi->state != ISI_STATE_IDLE);
  361. if (ret)
  362. return ret;
  363. if (isi->state != ISI_STATE_READY)
  364. return -EIO;
  365. spin_lock_irq(&isi->lock);
  366. isi->state = ISI_STATE_WAIT_SOF;
  367. isi_writel(isi, ISI_INTDIS, ISI_SR_VSYNC);
  368. spin_unlock_irq(&isi->lock);
  369. return 0;
  370. }
  371. /* abort streaming and wait for last buffer */
  372. static int stop_streaming(struct vb2_queue *vq)
  373. {
  374. struct soc_camera_device *icd = soc_camera_from_vb2q(vq);
  375. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  376. struct atmel_isi *isi = ici->priv;
  377. struct frame_buffer *buf, *node;
  378. int ret = 0;
  379. unsigned long timeout;
  380. spin_lock_irq(&isi->lock);
  381. isi->active = NULL;
  382. /* Release all active buffers */
  383. list_for_each_entry_safe(buf, node, &isi->video_buffer_list, list) {
  384. list_del_init(&buf->list);
  385. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  386. }
  387. spin_unlock_irq(&isi->lock);
  388. timeout = jiffies + FRAME_INTERVAL_MILLI_SEC * HZ;
  389. /* Wait until the end of the current frame. */
  390. while ((isi_readl(isi, ISI_STATUS) & ISI_CTRL_CDC) &&
  391. time_before(jiffies, timeout))
  392. msleep(1);
  393. if (time_after(jiffies, timeout)) {
  394. dev_err(icd->parent,
  395. "Timeout waiting for finishing codec request\n");
  396. return -ETIMEDOUT;
  397. }
  398. /* Disable interrupts */
  399. isi_writel(isi, ISI_INTDIS,
  400. ISI_SR_CXFR_DONE | ISI_SR_PXFR_DONE);
  401. /* Disable ISI and wait for it is done */
  402. ret = atmel_isi_wait_status(isi, WAIT_ISI_DISABLE);
  403. if (ret < 0)
  404. dev_err(icd->parent, "Disable ISI timed out\n");
  405. return ret;
  406. }
  407. static struct vb2_ops isi_video_qops = {
  408. .queue_setup = queue_setup,
  409. .buf_init = buffer_init,
  410. .buf_prepare = buffer_prepare,
  411. .buf_cleanup = buffer_cleanup,
  412. .buf_queue = buffer_queue,
  413. .start_streaming = start_streaming,
  414. .stop_streaming = stop_streaming,
  415. .wait_prepare = soc_camera_unlock,
  416. .wait_finish = soc_camera_lock,
  417. };
  418. /* ------------------------------------------------------------------
  419. SOC camera operations for the device
  420. ------------------------------------------------------------------*/
  421. static int isi_camera_init_videobuf(struct vb2_queue *q,
  422. struct soc_camera_device *icd)
  423. {
  424. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  425. q->io_modes = VB2_MMAP;
  426. q->drv_priv = icd;
  427. q->buf_struct_size = sizeof(struct frame_buffer);
  428. q->ops = &isi_video_qops;
  429. q->mem_ops = &vb2_dma_contig_memops;
  430. return vb2_queue_init(q);
  431. }
  432. static int isi_camera_set_fmt(struct soc_camera_device *icd,
  433. struct v4l2_format *f)
  434. {
  435. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  436. struct atmel_isi *isi = ici->priv;
  437. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  438. const struct soc_camera_format_xlate *xlate;
  439. struct v4l2_pix_format *pix = &f->fmt.pix;
  440. struct v4l2_mbus_framefmt mf;
  441. int ret;
  442. xlate = soc_camera_xlate_by_fourcc(icd, pix->pixelformat);
  443. if (!xlate) {
  444. dev_warn(icd->parent, "Format %x not found\n",
  445. pix->pixelformat);
  446. return -EINVAL;
  447. }
  448. dev_dbg(icd->parent, "Plan to set format %dx%d\n",
  449. pix->width, pix->height);
  450. mf.width = pix->width;
  451. mf.height = pix->height;
  452. mf.field = pix->field;
  453. mf.colorspace = pix->colorspace;
  454. mf.code = xlate->code;
  455. ret = v4l2_subdev_call(sd, video, s_mbus_fmt, &mf);
  456. if (ret < 0)
  457. return ret;
  458. if (mf.code != xlate->code)
  459. return -EINVAL;
  460. ret = configure_geometry(isi, pix->width, pix->height, xlate->code);
  461. if (ret < 0)
  462. return ret;
  463. pix->width = mf.width;
  464. pix->height = mf.height;
  465. pix->field = mf.field;
  466. pix->colorspace = mf.colorspace;
  467. icd->current_fmt = xlate;
  468. dev_dbg(icd->parent, "Finally set format %dx%d\n",
  469. pix->width, pix->height);
  470. return ret;
  471. }
  472. static int isi_camera_try_fmt(struct soc_camera_device *icd,
  473. struct v4l2_format *f)
  474. {
  475. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  476. const struct soc_camera_format_xlate *xlate;
  477. struct v4l2_pix_format *pix = &f->fmt.pix;
  478. struct v4l2_mbus_framefmt mf;
  479. u32 pixfmt = pix->pixelformat;
  480. int ret;
  481. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  482. if (pixfmt && !xlate) {
  483. dev_warn(icd->parent, "Format %x not found\n", pixfmt);
  484. return -EINVAL;
  485. }
  486. /* limit to Atmel ISI hardware capabilities */
  487. if (pix->height > MAX_SUPPORT_HEIGHT)
  488. pix->height = MAX_SUPPORT_HEIGHT;
  489. if (pix->width > MAX_SUPPORT_WIDTH)
  490. pix->width = MAX_SUPPORT_WIDTH;
  491. /* limit to sensor capabilities */
  492. mf.width = pix->width;
  493. mf.height = pix->height;
  494. mf.field = pix->field;
  495. mf.colorspace = pix->colorspace;
  496. mf.code = xlate->code;
  497. ret = v4l2_subdev_call(sd, video, try_mbus_fmt, &mf);
  498. if (ret < 0)
  499. return ret;
  500. pix->width = mf.width;
  501. pix->height = mf.height;
  502. pix->colorspace = mf.colorspace;
  503. switch (mf.field) {
  504. case V4L2_FIELD_ANY:
  505. pix->field = V4L2_FIELD_NONE;
  506. break;
  507. case V4L2_FIELD_NONE:
  508. break;
  509. default:
  510. dev_err(icd->parent, "Field type %d unsupported.\n",
  511. mf.field);
  512. ret = -EINVAL;
  513. }
  514. return ret;
  515. }
  516. static const struct soc_mbus_pixelfmt isi_camera_formats[] = {
  517. {
  518. .fourcc = V4L2_PIX_FMT_YUYV,
  519. .name = "Packed YUV422 16 bit",
  520. .bits_per_sample = 8,
  521. .packing = SOC_MBUS_PACKING_2X8_PADHI,
  522. .order = SOC_MBUS_ORDER_LE,
  523. },
  524. };
  525. /* This will be corrected as we get more formats */
  526. static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  527. {
  528. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  529. (fmt->bits_per_sample == 8 &&
  530. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  531. (fmt->bits_per_sample > 8 &&
  532. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  533. }
  534. static unsigned long make_bus_param(struct atmel_isi *isi)
  535. {
  536. unsigned long flags;
  537. /*
  538. * Platform specified synchronization and pixel clock polarities are
  539. * only a recommendation and are only used during probing. Atmel ISI
  540. * camera interface only works in master mode, i.e., uses HSYNC and
  541. * VSYNC signals from the sensor
  542. */
  543. flags = SOCAM_MASTER |
  544. SOCAM_HSYNC_ACTIVE_HIGH |
  545. SOCAM_HSYNC_ACTIVE_LOW |
  546. SOCAM_VSYNC_ACTIVE_HIGH |
  547. SOCAM_VSYNC_ACTIVE_LOW |
  548. SOCAM_PCLK_SAMPLE_RISING |
  549. SOCAM_PCLK_SAMPLE_FALLING |
  550. SOCAM_DATA_ACTIVE_HIGH;
  551. if (isi->pdata->data_width_flags & ISI_DATAWIDTH_10)
  552. flags |= SOCAM_DATAWIDTH_10;
  553. if (isi->pdata->data_width_flags & ISI_DATAWIDTH_8)
  554. flags |= SOCAM_DATAWIDTH_8;
  555. if (flags & SOCAM_DATAWIDTH_MASK)
  556. return flags;
  557. return 0;
  558. }
  559. static int isi_camera_try_bus_param(struct soc_camera_device *icd,
  560. unsigned char buswidth)
  561. {
  562. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  563. struct atmel_isi *isi = ici->priv;
  564. unsigned long camera_flags;
  565. int ret;
  566. camera_flags = icd->ops->query_bus_param(icd);
  567. ret = soc_camera_bus_param_compatible(camera_flags,
  568. make_bus_param(isi));
  569. if (!ret)
  570. return -EINVAL;
  571. return 0;
  572. }
  573. static int isi_camera_get_formats(struct soc_camera_device *icd,
  574. unsigned int idx,
  575. struct soc_camera_format_xlate *xlate)
  576. {
  577. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  578. int formats = 0, ret;
  579. /* sensor format */
  580. enum v4l2_mbus_pixelcode code;
  581. /* soc camera host format */
  582. const struct soc_mbus_pixelfmt *fmt;
  583. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  584. if (ret < 0)
  585. /* No more formats */
  586. return 0;
  587. fmt = soc_mbus_get_fmtdesc(code);
  588. if (!fmt) {
  589. dev_err(icd->parent,
  590. "Invalid format code #%u: %d\n", idx, code);
  591. return 0;
  592. }
  593. /* This also checks support for the requested bits-per-sample */
  594. ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample);
  595. if (ret < 0) {
  596. dev_err(icd->parent,
  597. "Fail to try the bus parameters.\n");
  598. return 0;
  599. }
  600. switch (code) {
  601. case V4L2_MBUS_FMT_UYVY8_2X8:
  602. case V4L2_MBUS_FMT_VYUY8_2X8:
  603. case V4L2_MBUS_FMT_YUYV8_2X8:
  604. case V4L2_MBUS_FMT_YVYU8_2X8:
  605. formats++;
  606. if (xlate) {
  607. xlate->host_fmt = &isi_camera_formats[0];
  608. xlate->code = code;
  609. xlate++;
  610. dev_dbg(icd->parent, "Providing format %s using code %d\n",
  611. isi_camera_formats[0].name, code);
  612. }
  613. break;
  614. default:
  615. if (!isi_camera_packing_supported(fmt))
  616. return 0;
  617. if (xlate)
  618. dev_dbg(icd->parent,
  619. "Providing format %s in pass-through mode\n",
  620. fmt->name);
  621. }
  622. /* Generic pass-through */
  623. formats++;
  624. if (xlate) {
  625. xlate->host_fmt = fmt;
  626. xlate->code = code;
  627. xlate++;
  628. }
  629. return formats;
  630. }
  631. /* Called with .video_lock held */
  632. static int isi_camera_add_device(struct soc_camera_device *icd)
  633. {
  634. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  635. struct atmel_isi *isi = ici->priv;
  636. int ret;
  637. if (isi->icd)
  638. return -EBUSY;
  639. ret = clk_enable(isi->pclk);
  640. if (ret)
  641. return ret;
  642. isi->icd = icd;
  643. dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n",
  644. icd->devnum);
  645. return 0;
  646. }
  647. /* Called with .video_lock held */
  648. static void isi_camera_remove_device(struct soc_camera_device *icd)
  649. {
  650. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  651. struct atmel_isi *isi = ici->priv;
  652. BUG_ON(icd != isi->icd);
  653. clk_disable(isi->pclk);
  654. isi->icd = NULL;
  655. dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n",
  656. icd->devnum);
  657. }
  658. static unsigned int isi_camera_poll(struct file *file, poll_table *pt)
  659. {
  660. struct soc_camera_device *icd = file->private_data;
  661. return vb2_poll(&icd->vb2_vidq, file, pt);
  662. }
  663. static int isi_camera_querycap(struct soc_camera_host *ici,
  664. struct v4l2_capability *cap)
  665. {
  666. strcpy(cap->driver, "atmel-isi");
  667. strcpy(cap->card, "Atmel Image Sensor Interface");
  668. cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE |
  669. V4L2_CAP_STREAMING);
  670. return 0;
  671. }
  672. static int isi_camera_set_bus_param(struct soc_camera_device *icd, u32 pixfmt)
  673. {
  674. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  675. struct atmel_isi *isi = ici->priv;
  676. unsigned long bus_flags, camera_flags, common_flags;
  677. int ret;
  678. u32 cfg1 = 0;
  679. camera_flags = icd->ops->query_bus_param(icd);
  680. bus_flags = make_bus_param(isi);
  681. common_flags = soc_camera_bus_param_compatible(camera_flags, bus_flags);
  682. dev_dbg(icd->parent, "Flags cam: 0x%lx host: 0x%lx common: 0x%lx\n",
  683. camera_flags, bus_flags, common_flags);
  684. if (!common_flags)
  685. return -EINVAL;
  686. /* Make choises, based on platform preferences */
  687. if ((common_flags & SOCAM_HSYNC_ACTIVE_HIGH) &&
  688. (common_flags & SOCAM_HSYNC_ACTIVE_LOW)) {
  689. if (isi->pdata->hsync_act_low)
  690. common_flags &= ~SOCAM_HSYNC_ACTIVE_HIGH;
  691. else
  692. common_flags &= ~SOCAM_HSYNC_ACTIVE_LOW;
  693. }
  694. if ((common_flags & SOCAM_VSYNC_ACTIVE_HIGH) &&
  695. (common_flags & SOCAM_VSYNC_ACTIVE_LOW)) {
  696. if (isi->pdata->vsync_act_low)
  697. common_flags &= ~SOCAM_VSYNC_ACTIVE_HIGH;
  698. else
  699. common_flags &= ~SOCAM_VSYNC_ACTIVE_LOW;
  700. }
  701. if ((common_flags & SOCAM_PCLK_SAMPLE_RISING) &&
  702. (common_flags & SOCAM_PCLK_SAMPLE_FALLING)) {
  703. if (isi->pdata->pclk_act_falling)
  704. common_flags &= ~SOCAM_PCLK_SAMPLE_RISING;
  705. else
  706. common_flags &= ~SOCAM_PCLK_SAMPLE_FALLING;
  707. }
  708. ret = icd->ops->set_bus_param(icd, common_flags);
  709. if (ret < 0) {
  710. dev_dbg(icd->parent, "Camera set_bus_param(%lx) returned %d\n",
  711. common_flags, ret);
  712. return ret;
  713. }
  714. /* set bus param for ISI */
  715. if (common_flags & SOCAM_HSYNC_ACTIVE_LOW)
  716. cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
  717. if (common_flags & SOCAM_VSYNC_ACTIVE_LOW)
  718. cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
  719. if (common_flags & SOCAM_PCLK_SAMPLE_FALLING)
  720. cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
  721. if (isi->pdata->has_emb_sync)
  722. cfg1 |= ISI_CFG1_EMB_SYNC;
  723. if (isi->pdata->isi_full_mode)
  724. cfg1 |= ISI_CFG1_FULL_MODE;
  725. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  726. isi_writel(isi, ISI_CFG1, cfg1);
  727. return 0;
  728. }
  729. static struct soc_camera_host_ops isi_soc_camera_host_ops = {
  730. .owner = THIS_MODULE,
  731. .add = isi_camera_add_device,
  732. .remove = isi_camera_remove_device,
  733. .set_fmt = isi_camera_set_fmt,
  734. .try_fmt = isi_camera_try_fmt,
  735. .get_formats = isi_camera_get_formats,
  736. .init_videobuf2 = isi_camera_init_videobuf,
  737. .poll = isi_camera_poll,
  738. .querycap = isi_camera_querycap,
  739. .set_bus_param = isi_camera_set_bus_param,
  740. };
  741. /* -----------------------------------------------------------------------*/
  742. static int __devexit atmel_isi_remove(struct platform_device *pdev)
  743. {
  744. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  745. struct atmel_isi *isi = container_of(soc_host,
  746. struct atmel_isi, soc_host);
  747. free_irq(isi->irq, isi);
  748. soc_camera_host_unregister(soc_host);
  749. vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
  750. dma_free_coherent(&pdev->dev,
  751. sizeof(struct fbd) * MAX_BUFFER_NUM,
  752. isi->p_fb_descriptors,
  753. isi->fb_descriptors_phys);
  754. iounmap(isi->regs);
  755. clk_put(isi->pclk);
  756. kfree(isi);
  757. return 0;
  758. }
  759. static int __devinit atmel_isi_probe(struct platform_device *pdev)
  760. {
  761. unsigned int irq;
  762. struct atmel_isi *isi;
  763. struct clk *pclk;
  764. struct resource *regs;
  765. int ret, i;
  766. struct device *dev = &pdev->dev;
  767. struct soc_camera_host *soc_host;
  768. struct isi_platform_data *pdata;
  769. pdata = dev->platform_data;
  770. if (!pdata || !pdata->data_width_flags) {
  771. dev_err(&pdev->dev,
  772. "No config available for Atmel ISI\n");
  773. return -EINVAL;
  774. }
  775. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  776. if (!regs)
  777. return -ENXIO;
  778. pclk = clk_get(&pdev->dev, "isi_clk");
  779. if (IS_ERR(pclk))
  780. return PTR_ERR(pclk);
  781. isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL);
  782. if (!isi) {
  783. ret = -ENOMEM;
  784. dev_err(&pdev->dev, "Can't allocate interface!\n");
  785. goto err_alloc_isi;
  786. }
  787. isi->pclk = pclk;
  788. isi->pdata = pdata;
  789. isi->active = NULL;
  790. spin_lock_init(&isi->lock);
  791. init_waitqueue_head(&isi->vsync_wq);
  792. INIT_LIST_HEAD(&isi->video_buffer_list);
  793. INIT_LIST_HEAD(&isi->dma_desc_head);
  794. isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
  795. sizeof(struct fbd) * MAX_BUFFER_NUM,
  796. &isi->fb_descriptors_phys,
  797. GFP_KERNEL);
  798. if (!isi->p_fb_descriptors) {
  799. ret = -ENOMEM;
  800. dev_err(&pdev->dev, "Can't allocate descriptors!\n");
  801. goto err_alloc_descriptors;
  802. }
  803. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  804. isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
  805. isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
  806. i * sizeof(struct fbd);
  807. list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
  808. }
  809. isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  810. if (IS_ERR(isi->alloc_ctx)) {
  811. ret = PTR_ERR(isi->alloc_ctx);
  812. goto err_alloc_ctx;
  813. }
  814. isi->regs = ioremap(regs->start, resource_size(regs));
  815. if (!isi->regs) {
  816. ret = -ENOMEM;
  817. goto err_ioremap;
  818. }
  819. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  820. irq = platform_get_irq(pdev, 0);
  821. if (irq < 0) {
  822. ret = irq;
  823. goto err_req_irq;
  824. }
  825. ret = request_irq(irq, isi_interrupt, 0, "isi", isi);
  826. if (ret) {
  827. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  828. goto err_req_irq;
  829. }
  830. isi->irq = irq;
  831. soc_host = &isi->soc_host;
  832. soc_host->drv_name = "isi-camera";
  833. soc_host->ops = &isi_soc_camera_host_ops;
  834. soc_host->priv = isi;
  835. soc_host->v4l2_dev.dev = &pdev->dev;
  836. soc_host->nr = pdev->id;
  837. ret = soc_camera_host_register(soc_host);
  838. if (ret) {
  839. dev_err(&pdev->dev, "Unable to register soc camera host\n");
  840. goto err_register_soc_camera_host;
  841. }
  842. return 0;
  843. err_register_soc_camera_host:
  844. free_irq(isi->irq, isi);
  845. err_req_irq:
  846. iounmap(isi->regs);
  847. err_ioremap:
  848. vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
  849. err_alloc_ctx:
  850. dma_free_coherent(&pdev->dev,
  851. sizeof(struct fbd) * MAX_BUFFER_NUM,
  852. isi->p_fb_descriptors,
  853. isi->fb_descriptors_phys);
  854. err_alloc_descriptors:
  855. kfree(isi);
  856. err_alloc_isi:
  857. clk_put(isi->pclk);
  858. return ret;
  859. }
  860. static struct platform_driver atmel_isi_driver = {
  861. .probe = atmel_isi_probe,
  862. .remove = __devexit_p(atmel_isi_remove),
  863. .driver = {
  864. .name = "atmel_isi",
  865. .owner = THIS_MODULE,
  866. },
  867. };
  868. static int __init atmel_isi_init_module(void)
  869. {
  870. return platform_driver_probe(&atmel_isi_driver, &atmel_isi_probe);
  871. }
  872. static void __exit atmel_isi_exit(void)
  873. {
  874. platform_driver_unregister(&atmel_isi_driver);
  875. }
  876. module_init(atmel_isi_init_module);
  877. module_exit(atmel_isi_exit);
  878. MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
  879. MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
  880. MODULE_LICENSE("GPL");
  881. MODULE_SUPPORTED_DEVICE("video");