atmel-isi.c 28 KB

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