atmel-isi.c 28 KB

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