atmel-isi.c 26 KB

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