atmel-isi.c 27 KB

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