atmel-isi.c 28 KB

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