atmel-isi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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. do_gettimeofday(&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. },
  530. };
  531. /* This will be corrected as we get more formats */
  532. static bool isi_camera_packing_supported(const struct soc_mbus_pixelfmt *fmt)
  533. {
  534. return fmt->packing == SOC_MBUS_PACKING_NONE ||
  535. (fmt->bits_per_sample == 8 &&
  536. fmt->packing == SOC_MBUS_PACKING_2X8_PADHI) ||
  537. (fmt->bits_per_sample > 8 &&
  538. fmt->packing == SOC_MBUS_PACKING_EXTEND16);
  539. }
  540. #define ISI_BUS_PARAM (V4L2_MBUS_MASTER | \
  541. V4L2_MBUS_HSYNC_ACTIVE_HIGH | \
  542. V4L2_MBUS_HSYNC_ACTIVE_LOW | \
  543. V4L2_MBUS_VSYNC_ACTIVE_HIGH | \
  544. V4L2_MBUS_VSYNC_ACTIVE_LOW | \
  545. V4L2_MBUS_PCLK_SAMPLE_RISING | \
  546. V4L2_MBUS_PCLK_SAMPLE_FALLING | \
  547. V4L2_MBUS_DATA_ACTIVE_HIGH)
  548. static int isi_camera_try_bus_param(struct soc_camera_device *icd,
  549. unsigned char buswidth)
  550. {
  551. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  552. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  553. struct atmel_isi *isi = ici->priv;
  554. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  555. unsigned long common_flags;
  556. int ret;
  557. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  558. if (!ret) {
  559. common_flags = soc_mbus_config_compatible(&cfg,
  560. ISI_BUS_PARAM);
  561. if (!common_flags) {
  562. dev_warn(icd->parent,
  563. "Flags incompatible: camera 0x%x, host 0x%x\n",
  564. cfg.flags, ISI_BUS_PARAM);
  565. return -EINVAL;
  566. }
  567. } else if (ret != -ENOIOCTLCMD) {
  568. return ret;
  569. }
  570. if ((1 << (buswidth - 1)) & isi->width_flags)
  571. return 0;
  572. return -EINVAL;
  573. }
  574. static int isi_camera_get_formats(struct soc_camera_device *icd,
  575. unsigned int idx,
  576. struct soc_camera_format_xlate *xlate)
  577. {
  578. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  579. int formats = 0, ret;
  580. /* sensor format */
  581. enum v4l2_mbus_pixelcode code;
  582. /* soc camera host format */
  583. const struct soc_mbus_pixelfmt *fmt;
  584. ret = v4l2_subdev_call(sd, video, enum_mbus_fmt, idx, &code);
  585. if (ret < 0)
  586. /* No more formats */
  587. return 0;
  588. fmt = soc_mbus_get_fmtdesc(code);
  589. if (!fmt) {
  590. dev_err(icd->parent,
  591. "Invalid format code #%u: %d\n", idx, code);
  592. return 0;
  593. }
  594. /* This also checks support for the requested bits-per-sample */
  595. ret = isi_camera_try_bus_param(icd, fmt->bits_per_sample);
  596. if (ret < 0) {
  597. dev_err(icd->parent,
  598. "Fail to try the bus parameters.\n");
  599. return 0;
  600. }
  601. switch (code) {
  602. case V4L2_MBUS_FMT_UYVY8_2X8:
  603. case V4L2_MBUS_FMT_VYUY8_2X8:
  604. case V4L2_MBUS_FMT_YUYV8_2X8:
  605. case V4L2_MBUS_FMT_YVYU8_2X8:
  606. formats++;
  607. if (xlate) {
  608. xlate->host_fmt = &isi_camera_formats[0];
  609. xlate->code = code;
  610. xlate++;
  611. dev_dbg(icd->parent, "Providing format %s using code %d\n",
  612. isi_camera_formats[0].name, code);
  613. }
  614. break;
  615. default:
  616. if (!isi_camera_packing_supported(fmt))
  617. return 0;
  618. if (xlate)
  619. dev_dbg(icd->parent,
  620. "Providing format %s in pass-through mode\n",
  621. fmt->name);
  622. }
  623. /* Generic pass-through */
  624. formats++;
  625. if (xlate) {
  626. xlate->host_fmt = fmt;
  627. xlate->code = code;
  628. xlate++;
  629. }
  630. return formats;
  631. }
  632. /* Called with .video_lock held */
  633. static int isi_camera_add_device(struct soc_camera_device *icd)
  634. {
  635. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  636. struct atmel_isi *isi = ici->priv;
  637. int ret;
  638. if (isi->icd)
  639. return -EBUSY;
  640. ret = clk_enable(isi->pclk);
  641. if (ret)
  642. return ret;
  643. ret = clk_enable(isi->mck);
  644. if (ret) {
  645. clk_disable(isi->pclk);
  646. return ret;
  647. }
  648. isi->icd = icd;
  649. dev_dbg(icd->parent, "Atmel ISI Camera driver attached to camera %d\n",
  650. icd->devnum);
  651. return 0;
  652. }
  653. /* Called with .video_lock held */
  654. static void isi_camera_remove_device(struct soc_camera_device *icd)
  655. {
  656. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  657. struct atmel_isi *isi = ici->priv;
  658. BUG_ON(icd != isi->icd);
  659. clk_disable(isi->mck);
  660. clk_disable(isi->pclk);
  661. isi->icd = NULL;
  662. dev_dbg(icd->parent, "Atmel ISI Camera driver detached from camera %d\n",
  663. icd->devnum);
  664. }
  665. static unsigned int isi_camera_poll(struct file *file, poll_table *pt)
  666. {
  667. struct soc_camera_device *icd = file->private_data;
  668. return vb2_poll(&icd->vb2_vidq, file, pt);
  669. }
  670. static int isi_camera_querycap(struct soc_camera_host *ici,
  671. struct v4l2_capability *cap)
  672. {
  673. strcpy(cap->driver, "atmel-isi");
  674. strcpy(cap->card, "Atmel Image Sensor Interface");
  675. cap->capabilities = (V4L2_CAP_VIDEO_CAPTURE |
  676. V4L2_CAP_STREAMING);
  677. return 0;
  678. }
  679. static int isi_camera_set_bus_param(struct soc_camera_device *icd)
  680. {
  681. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  682. struct soc_camera_host *ici = to_soc_camera_host(icd->parent);
  683. struct atmel_isi *isi = ici->priv;
  684. struct v4l2_mbus_config cfg = {.type = V4L2_MBUS_PARALLEL,};
  685. unsigned long common_flags;
  686. int ret;
  687. u32 cfg1 = 0;
  688. ret = v4l2_subdev_call(sd, video, g_mbus_config, &cfg);
  689. if (!ret) {
  690. common_flags = soc_mbus_config_compatible(&cfg,
  691. ISI_BUS_PARAM);
  692. if (!common_flags) {
  693. dev_warn(icd->parent,
  694. "Flags incompatible: camera 0x%x, host 0x%x\n",
  695. cfg.flags, ISI_BUS_PARAM);
  696. return -EINVAL;
  697. }
  698. } else if (ret != -ENOIOCTLCMD) {
  699. return ret;
  700. } else {
  701. common_flags = ISI_BUS_PARAM;
  702. }
  703. dev_dbg(icd->parent, "Flags cam: 0x%x host: 0x%x common: 0x%lx\n",
  704. cfg.flags, ISI_BUS_PARAM, common_flags);
  705. /* Make choises, based on platform preferences */
  706. if ((common_flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) &&
  707. (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)) {
  708. if (isi->pdata->hsync_act_low)
  709. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_HIGH;
  710. else
  711. common_flags &= ~V4L2_MBUS_HSYNC_ACTIVE_LOW;
  712. }
  713. if ((common_flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH) &&
  714. (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)) {
  715. if (isi->pdata->vsync_act_low)
  716. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_HIGH;
  717. else
  718. common_flags &= ~V4L2_MBUS_VSYNC_ACTIVE_LOW;
  719. }
  720. if ((common_flags & V4L2_MBUS_PCLK_SAMPLE_RISING) &&
  721. (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)) {
  722. if (isi->pdata->pclk_act_falling)
  723. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_RISING;
  724. else
  725. common_flags &= ~V4L2_MBUS_PCLK_SAMPLE_FALLING;
  726. }
  727. cfg.flags = common_flags;
  728. ret = v4l2_subdev_call(sd, video, s_mbus_config, &cfg);
  729. if (ret < 0 && ret != -ENOIOCTLCMD) {
  730. dev_dbg(icd->parent, "camera s_mbus_config(0x%lx) returned %d\n",
  731. common_flags, ret);
  732. return ret;
  733. }
  734. /* set bus param for ISI */
  735. if (common_flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  736. cfg1 |= ISI_CFG1_HSYNC_POL_ACTIVE_LOW;
  737. if (common_flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
  738. cfg1 |= ISI_CFG1_VSYNC_POL_ACTIVE_LOW;
  739. if (common_flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  740. cfg1 |= ISI_CFG1_PIXCLK_POL_ACTIVE_FALLING;
  741. if (isi->pdata->has_emb_sync)
  742. cfg1 |= ISI_CFG1_EMB_SYNC;
  743. if (isi->pdata->full_mode)
  744. cfg1 |= ISI_CFG1_FULL_MODE;
  745. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  746. isi_writel(isi, ISI_CFG1, cfg1);
  747. return 0;
  748. }
  749. static struct soc_camera_host_ops isi_soc_camera_host_ops = {
  750. .owner = THIS_MODULE,
  751. .add = isi_camera_add_device,
  752. .remove = isi_camera_remove_device,
  753. .set_fmt = isi_camera_set_fmt,
  754. .try_fmt = isi_camera_try_fmt,
  755. .get_formats = isi_camera_get_formats,
  756. .init_videobuf2 = isi_camera_init_videobuf,
  757. .poll = isi_camera_poll,
  758. .querycap = isi_camera_querycap,
  759. .set_bus_param = isi_camera_set_bus_param,
  760. };
  761. /* -----------------------------------------------------------------------*/
  762. static int __devexit atmel_isi_remove(struct platform_device *pdev)
  763. {
  764. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  765. struct atmel_isi *isi = container_of(soc_host,
  766. struct atmel_isi, soc_host);
  767. free_irq(isi->irq, isi);
  768. soc_camera_host_unregister(soc_host);
  769. vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
  770. dma_free_coherent(&pdev->dev,
  771. sizeof(struct fbd) * MAX_BUFFER_NUM,
  772. isi->p_fb_descriptors,
  773. isi->fb_descriptors_phys);
  774. iounmap(isi->regs);
  775. clk_unprepare(isi->mck);
  776. clk_put(isi->mck);
  777. clk_unprepare(isi->pclk);
  778. clk_put(isi->pclk);
  779. kfree(isi);
  780. return 0;
  781. }
  782. static int __devinit atmel_isi_probe(struct platform_device *pdev)
  783. {
  784. unsigned int irq;
  785. struct atmel_isi *isi;
  786. struct clk *pclk;
  787. struct resource *regs;
  788. int ret, i;
  789. struct device *dev = &pdev->dev;
  790. struct soc_camera_host *soc_host;
  791. struct isi_platform_data *pdata;
  792. pdata = dev->platform_data;
  793. if (!pdata || !pdata->data_width_flags || !pdata->mck_hz) {
  794. dev_err(&pdev->dev,
  795. "No config available for Atmel ISI\n");
  796. return -EINVAL;
  797. }
  798. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  799. if (!regs)
  800. return -ENXIO;
  801. pclk = clk_get(&pdev->dev, "isi_clk");
  802. if (IS_ERR(pclk))
  803. return PTR_ERR(pclk);
  804. ret = clk_prepare(pclk);
  805. if (ret)
  806. goto err_clk_prepare_pclk;
  807. isi = kzalloc(sizeof(struct atmel_isi), GFP_KERNEL);
  808. if (!isi) {
  809. ret = -ENOMEM;
  810. dev_err(&pdev->dev, "Can't allocate interface!\n");
  811. goto err_alloc_isi;
  812. }
  813. isi->pclk = pclk;
  814. isi->pdata = pdata;
  815. isi->active = NULL;
  816. spin_lock_init(&isi->lock);
  817. init_waitqueue_head(&isi->vsync_wq);
  818. INIT_LIST_HEAD(&isi->video_buffer_list);
  819. INIT_LIST_HEAD(&isi->dma_desc_head);
  820. /* Get ISI_MCK, provided by programmable clock or external clock */
  821. isi->mck = clk_get(dev, "isi_mck");
  822. if (IS_ERR(isi->mck)) {
  823. dev_err(dev, "Failed to get isi_mck\n");
  824. ret = PTR_ERR(isi->mck);
  825. goto err_clk_get;
  826. }
  827. ret = clk_prepare(isi->mck);
  828. if (ret)
  829. goto err_clk_prepare_mck;
  830. /* Set ISI_MCK's frequency, it should be faster than pixel clock */
  831. ret = clk_set_rate(isi->mck, pdata->mck_hz);
  832. if (ret < 0)
  833. goto err_set_mck_rate;
  834. isi->p_fb_descriptors = dma_alloc_coherent(&pdev->dev,
  835. sizeof(struct fbd) * MAX_BUFFER_NUM,
  836. &isi->fb_descriptors_phys,
  837. GFP_KERNEL);
  838. if (!isi->p_fb_descriptors) {
  839. ret = -ENOMEM;
  840. dev_err(&pdev->dev, "Can't allocate descriptors!\n");
  841. goto err_alloc_descriptors;
  842. }
  843. for (i = 0; i < MAX_BUFFER_NUM; i++) {
  844. isi->dma_desc[i].p_fbd = isi->p_fb_descriptors + i;
  845. isi->dma_desc[i].fbd_phys = isi->fb_descriptors_phys +
  846. i * sizeof(struct fbd);
  847. list_add(&isi->dma_desc[i].list, &isi->dma_desc_head);
  848. }
  849. isi->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  850. if (IS_ERR(isi->alloc_ctx)) {
  851. ret = PTR_ERR(isi->alloc_ctx);
  852. goto err_alloc_ctx;
  853. }
  854. isi->regs = ioremap(regs->start, resource_size(regs));
  855. if (!isi->regs) {
  856. ret = -ENOMEM;
  857. goto err_ioremap;
  858. }
  859. if (pdata->data_width_flags & ISI_DATAWIDTH_8)
  860. isi->width_flags = 1 << 7;
  861. if (pdata->data_width_flags & ISI_DATAWIDTH_10)
  862. isi->width_flags |= 1 << 9;
  863. isi_writel(isi, ISI_CTRL, ISI_CTRL_DIS);
  864. irq = platform_get_irq(pdev, 0);
  865. if (irq < 0) {
  866. ret = irq;
  867. goto err_req_irq;
  868. }
  869. ret = request_irq(irq, isi_interrupt, 0, "isi", isi);
  870. if (ret) {
  871. dev_err(&pdev->dev, "Unable to request irq %d\n", irq);
  872. goto err_req_irq;
  873. }
  874. isi->irq = irq;
  875. soc_host = &isi->soc_host;
  876. soc_host->drv_name = "isi-camera";
  877. soc_host->ops = &isi_soc_camera_host_ops;
  878. soc_host->priv = isi;
  879. soc_host->v4l2_dev.dev = &pdev->dev;
  880. soc_host->nr = pdev->id;
  881. ret = soc_camera_host_register(soc_host);
  882. if (ret) {
  883. dev_err(&pdev->dev, "Unable to register soc camera host\n");
  884. goto err_register_soc_camera_host;
  885. }
  886. return 0;
  887. err_register_soc_camera_host:
  888. free_irq(isi->irq, isi);
  889. err_req_irq:
  890. iounmap(isi->regs);
  891. err_ioremap:
  892. vb2_dma_contig_cleanup_ctx(isi->alloc_ctx);
  893. err_alloc_ctx:
  894. dma_free_coherent(&pdev->dev,
  895. sizeof(struct fbd) * MAX_BUFFER_NUM,
  896. isi->p_fb_descriptors,
  897. isi->fb_descriptors_phys);
  898. err_alloc_descriptors:
  899. err_set_mck_rate:
  900. clk_unprepare(isi->mck);
  901. err_clk_prepare_mck:
  902. clk_put(isi->mck);
  903. err_clk_get:
  904. kfree(isi);
  905. err_alloc_isi:
  906. clk_unprepare(pclk);
  907. err_clk_prepare_pclk:
  908. clk_put(pclk);
  909. return ret;
  910. }
  911. static struct platform_driver atmel_isi_driver = {
  912. .probe = atmel_isi_probe,
  913. .remove = __devexit_p(atmel_isi_remove),
  914. .driver = {
  915. .name = "atmel_isi",
  916. .owner = THIS_MODULE,
  917. },
  918. };
  919. static int __init atmel_isi_init_module(void)
  920. {
  921. return platform_driver_probe(&atmel_isi_driver, &atmel_isi_probe);
  922. }
  923. static void __exit atmel_isi_exit(void)
  924. {
  925. platform_driver_unregister(&atmel_isi_driver);
  926. }
  927. module_init(atmel_isi_init_module);
  928. module_exit(atmel_isi_exit);
  929. MODULE_AUTHOR("Josh Wu <josh.wu@atmel.com>");
  930. MODULE_DESCRIPTION("The V4L2 driver for Atmel Linux");
  931. MODULE_LICENSE("GPL");
  932. MODULE_SUPPORTED_DEVICE("video");