sh_mobile_ceu_camera.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * V4L2 Driver for SuperH Mobile CEU interface
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * Based on V4L2 Driver for PXA camera host - "pxa_camera.c",
  7. *
  8. * Copyright (C) 2006, Sascha Hauer, Pengutronix
  9. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/io.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/errno.h>
  22. #include <linux/fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/mm.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/time.h>
  28. #include <linux/version.h>
  29. #include <linux/device.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/clk.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-dev.h>
  35. #include <media/soc_camera.h>
  36. #include <media/sh_mobile_ceu.h>
  37. #include <media/videobuf-dma-contig.h>
  38. /* register offsets for sh7722 / sh7723 */
  39. #define CAPSR 0x00 /* Capture start register */
  40. #define CAPCR 0x04 /* Capture control register */
  41. #define CAMCR 0x08 /* Capture interface control register */
  42. #define CMCYR 0x0c /* Capture interface cycle register */
  43. #define CAMOR 0x10 /* Capture interface offset register */
  44. #define CAPWR 0x14 /* Capture interface width register */
  45. #define CAIFR 0x18 /* Capture interface input format register */
  46. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  47. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  48. #define CRCNTR 0x28 /* CEU register control register */
  49. #define CRCMPR 0x2c /* CEU register forcible control register */
  50. #define CFLCR 0x30 /* Capture filter control register */
  51. #define CFSZR 0x34 /* Capture filter size clip register */
  52. #define CDWDR 0x38 /* Capture destination width register */
  53. #define CDAYR 0x3c /* Capture data address Y register */
  54. #define CDACR 0x40 /* Capture data address C register */
  55. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  56. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  57. #define CBDSR 0x4c /* Capture bundle destination size register */
  58. #define CFWCR 0x5c /* Firewall operation control register */
  59. #define CLFCR 0x60 /* Capture low-pass filter control register */
  60. #define CDOCR 0x64 /* Capture data output control register */
  61. #define CDDCR 0x68 /* Capture data complexity level register */
  62. #define CDDAR 0x6c /* Capture data complexity level address register */
  63. #define CEIER 0x70 /* Capture event interrupt enable register */
  64. #define CETCR 0x74 /* Capture event flag clear register */
  65. #define CSTSR 0x7c /* Capture status register */
  66. #define CSRTR 0x80 /* Capture software reset register */
  67. #define CDSSR 0x84 /* Capture data size register */
  68. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  69. #define CDACR2 0x94 /* Capture data address C register 2 */
  70. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  71. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  72. /* per video frame buffer */
  73. struct sh_mobile_ceu_buffer {
  74. struct videobuf_buffer vb; /* v4l buffer must be first */
  75. const struct soc_camera_data_format *fmt;
  76. };
  77. struct sh_mobile_ceu_dev {
  78. struct device *dev;
  79. struct soc_camera_host ici;
  80. struct soc_camera_device *icd;
  81. unsigned int irq;
  82. void __iomem *base;
  83. struct clk *clk;
  84. unsigned long video_limit;
  85. /* lock used to protect videobuf */
  86. spinlock_t lock;
  87. struct list_head capture;
  88. struct videobuf_buffer *active;
  89. int is_interlaced;
  90. struct sh_mobile_ceu_info *pdata;
  91. const struct soc_camera_data_format *camera_fmt;
  92. };
  93. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  94. {
  95. unsigned long flags;
  96. flags = SOCAM_MASTER |
  97. SOCAM_PCLK_SAMPLE_RISING |
  98. SOCAM_HSYNC_ACTIVE_HIGH |
  99. SOCAM_HSYNC_ACTIVE_LOW |
  100. SOCAM_VSYNC_ACTIVE_HIGH |
  101. SOCAM_VSYNC_ACTIVE_LOW |
  102. SOCAM_DATA_ACTIVE_HIGH;
  103. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  104. flags |= SOCAM_DATAWIDTH_8;
  105. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  106. flags |= SOCAM_DATAWIDTH_16;
  107. if (flags & SOCAM_DATAWIDTH_MASK)
  108. return flags;
  109. return 0;
  110. }
  111. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  112. unsigned long reg_offs, u32 data)
  113. {
  114. iowrite32(data, priv->base + reg_offs);
  115. }
  116. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  117. {
  118. return ioread32(priv->base + reg_offs);
  119. }
  120. /*
  121. * Videobuf operations
  122. */
  123. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  124. unsigned int *count,
  125. unsigned int *size)
  126. {
  127. struct soc_camera_device *icd = vq->priv_data;
  128. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  129. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  130. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  131. *size = PAGE_ALIGN(icd->width * icd->height * bytes_per_pixel);
  132. if (0 == *count)
  133. *count = 2;
  134. if (pcdev->video_limit) {
  135. while (*size * *count > pcdev->video_limit)
  136. (*count)--;
  137. }
  138. dev_dbg(&icd->dev, "count=%d, size=%d\n", *count, *size);
  139. return 0;
  140. }
  141. static void free_buffer(struct videobuf_queue *vq,
  142. struct sh_mobile_ceu_buffer *buf)
  143. {
  144. struct soc_camera_device *icd = vq->priv_data;
  145. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  146. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  147. if (in_interrupt())
  148. BUG();
  149. videobuf_waiton(&buf->vb, 0, 0);
  150. videobuf_dma_contig_free(vq, &buf->vb);
  151. dev_dbg(&icd->dev, "%s freed\n", __func__);
  152. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  153. }
  154. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  155. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  156. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  157. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  158. static void sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  159. {
  160. struct soc_camera_device *icd = pcdev->icd;
  161. dma_addr_t phys_addr_top, phys_addr_bottom;
  162. /* The hardware is _very_ picky about this sequence. Especially
  163. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  164. * several not-so-well documented interrupt sources in CETCR.
  165. */
  166. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_CPEIE);
  167. ceu_write(pcdev, CETCR, ~ceu_read(pcdev, CETCR) & CEU_CETCR_MAGIC);
  168. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_CPEIE);
  169. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  170. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  171. if (!pcdev->active)
  172. return;
  173. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  174. ceu_write(pcdev, CDAYR, phys_addr_top);
  175. if (pcdev->is_interlaced) {
  176. phys_addr_bottom = phys_addr_top + icd->width;
  177. ceu_write(pcdev, CDBYR, phys_addr_bottom);
  178. }
  179. switch (icd->current_fmt->fourcc) {
  180. case V4L2_PIX_FMT_NV12:
  181. case V4L2_PIX_FMT_NV21:
  182. case V4L2_PIX_FMT_NV16:
  183. case V4L2_PIX_FMT_NV61:
  184. phys_addr_top += icd->width * icd->height;
  185. ceu_write(pcdev, CDACR, phys_addr_top);
  186. if (pcdev->is_interlaced) {
  187. phys_addr_bottom = phys_addr_top + icd->width;
  188. ceu_write(pcdev, CDBCR, phys_addr_bottom);
  189. }
  190. }
  191. pcdev->active->state = VIDEOBUF_ACTIVE;
  192. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  193. }
  194. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  195. struct videobuf_buffer *vb,
  196. enum v4l2_field field)
  197. {
  198. struct soc_camera_device *icd = vq->priv_data;
  199. struct sh_mobile_ceu_buffer *buf;
  200. int ret;
  201. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  202. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  203. vb, vb->baddr, vb->bsize);
  204. /* Added list head initialization on alloc */
  205. WARN_ON(!list_empty(&vb->queue));
  206. #ifdef DEBUG
  207. /* This can be useful if you want to see if we actually fill
  208. * the buffer with something */
  209. memset((void *)vb->baddr, 0xaa, vb->bsize);
  210. #endif
  211. BUG_ON(NULL == icd->current_fmt);
  212. if (buf->fmt != icd->current_fmt ||
  213. vb->width != icd->width ||
  214. vb->height != icd->height ||
  215. vb->field != field) {
  216. buf->fmt = icd->current_fmt;
  217. vb->width = icd->width;
  218. vb->height = icd->height;
  219. vb->field = field;
  220. vb->state = VIDEOBUF_NEEDS_INIT;
  221. }
  222. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  223. if (0 != vb->baddr && vb->bsize < vb->size) {
  224. ret = -EINVAL;
  225. goto out;
  226. }
  227. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  228. ret = videobuf_iolock(vq, vb, NULL);
  229. if (ret)
  230. goto fail;
  231. vb->state = VIDEOBUF_PREPARED;
  232. }
  233. return 0;
  234. fail:
  235. free_buffer(vq, buf);
  236. out:
  237. return ret;
  238. }
  239. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  240. struct videobuf_buffer *vb)
  241. {
  242. struct soc_camera_device *icd = vq->priv_data;
  243. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  244. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  245. unsigned long flags;
  246. dev_dbg(&icd->dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  247. vb, vb->baddr, vb->bsize);
  248. vb->state = VIDEOBUF_QUEUED;
  249. spin_lock_irqsave(&pcdev->lock, flags);
  250. list_add_tail(&vb->queue, &pcdev->capture);
  251. if (!pcdev->active) {
  252. pcdev->active = vb;
  253. sh_mobile_ceu_capture(pcdev);
  254. }
  255. spin_unlock_irqrestore(&pcdev->lock, flags);
  256. }
  257. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  258. struct videobuf_buffer *vb)
  259. {
  260. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  261. }
  262. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  263. .buf_setup = sh_mobile_ceu_videobuf_setup,
  264. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  265. .buf_queue = sh_mobile_ceu_videobuf_queue,
  266. .buf_release = sh_mobile_ceu_videobuf_release,
  267. };
  268. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  269. {
  270. struct sh_mobile_ceu_dev *pcdev = data;
  271. struct videobuf_buffer *vb;
  272. unsigned long flags;
  273. spin_lock_irqsave(&pcdev->lock, flags);
  274. vb = pcdev->active;
  275. list_del_init(&vb->queue);
  276. if (!list_empty(&pcdev->capture))
  277. pcdev->active = list_entry(pcdev->capture.next,
  278. struct videobuf_buffer, queue);
  279. else
  280. pcdev->active = NULL;
  281. sh_mobile_ceu_capture(pcdev);
  282. vb->state = VIDEOBUF_DONE;
  283. do_gettimeofday(&vb->ts);
  284. vb->field_count++;
  285. wake_up(&vb->done);
  286. spin_unlock_irqrestore(&pcdev->lock, flags);
  287. return IRQ_HANDLED;
  288. }
  289. /* Called with .video_lock held */
  290. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  291. {
  292. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  293. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  294. int ret = -EBUSY;
  295. if (pcdev->icd)
  296. goto err;
  297. dev_info(&icd->dev,
  298. "SuperH Mobile CEU driver attached to camera %d\n",
  299. icd->devnum);
  300. ret = icd->ops->init(icd);
  301. if (ret)
  302. goto err;
  303. clk_enable(pcdev->clk);
  304. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  305. while (ceu_read(pcdev, CSTSR) & 1)
  306. msleep(1);
  307. pcdev->icd = icd;
  308. err:
  309. return ret;
  310. }
  311. /* Called with .video_lock held */
  312. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  313. {
  314. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  315. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  316. unsigned long flags;
  317. BUG_ON(icd != pcdev->icd);
  318. /* disable capture, disable interrupts */
  319. ceu_write(pcdev, CEIER, 0);
  320. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  321. /* make sure active buffer is canceled */
  322. spin_lock_irqsave(&pcdev->lock, flags);
  323. if (pcdev->active) {
  324. list_del(&pcdev->active->queue);
  325. pcdev->active->state = VIDEOBUF_ERROR;
  326. wake_up_all(&pcdev->active->done);
  327. pcdev->active = NULL;
  328. }
  329. spin_unlock_irqrestore(&pcdev->lock, flags);
  330. clk_disable(pcdev->clk);
  331. icd->ops->release(icd);
  332. dev_info(&icd->dev,
  333. "SuperH Mobile CEU driver detached from camera %d\n",
  334. icd->devnum);
  335. pcdev->icd = NULL;
  336. }
  337. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  338. __u32 pixfmt)
  339. {
  340. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  341. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  342. int ret, buswidth, width, height, cfszr_width, cdwdr_width;
  343. unsigned long camera_flags, common_flags, value;
  344. int yuv_mode, yuv_lineskip;
  345. camera_flags = icd->ops->query_bus_param(icd);
  346. common_flags = soc_camera_bus_param_compatible(camera_flags,
  347. make_bus_param(pcdev));
  348. if (!common_flags)
  349. return -EINVAL;
  350. ret = icd->ops->set_bus_param(icd, common_flags);
  351. if (ret < 0)
  352. return ret;
  353. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  354. case SOCAM_DATAWIDTH_8:
  355. buswidth = 8;
  356. break;
  357. case SOCAM_DATAWIDTH_16:
  358. buswidth = 16;
  359. break;
  360. default:
  361. return -EINVAL;
  362. }
  363. ceu_write(pcdev, CRCNTR, 0);
  364. ceu_write(pcdev, CRCMPR, 0);
  365. value = 0x00000010; /* data fetch by default */
  366. yuv_mode = yuv_lineskip = 0;
  367. switch (icd->current_fmt->fourcc) {
  368. case V4L2_PIX_FMT_NV12:
  369. case V4L2_PIX_FMT_NV21:
  370. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  371. /* fall-through */
  372. case V4L2_PIX_FMT_NV16:
  373. case V4L2_PIX_FMT_NV61:
  374. yuv_mode = 1;
  375. switch (pcdev->camera_fmt->fourcc) {
  376. case V4L2_PIX_FMT_UYVY:
  377. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  378. break;
  379. case V4L2_PIX_FMT_VYUY:
  380. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  381. break;
  382. case V4L2_PIX_FMT_YUYV:
  383. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  384. break;
  385. case V4L2_PIX_FMT_YVYU:
  386. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  387. break;
  388. default:
  389. BUG();
  390. }
  391. }
  392. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  393. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  394. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  395. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  396. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  397. value |= buswidth == 16 ? 1 << 12 : 0;
  398. ceu_write(pcdev, CAMCR, value);
  399. ceu_write(pcdev, CAPCR, 0x00300000);
  400. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  401. mdelay(1);
  402. if (yuv_mode) {
  403. width = icd->width * 2;
  404. width = buswidth == 16 ? width / 2 : width;
  405. cfszr_width = cdwdr_width = icd->width;
  406. } else {
  407. width = icd->width * ((icd->current_fmt->depth + 7) >> 3);
  408. width = buswidth == 16 ? width / 2 : width;
  409. cfszr_width = buswidth == 8 ? width / 2 : width;
  410. cdwdr_width = buswidth == 16 ? width * 2 : width;
  411. }
  412. height = icd->height;
  413. if (pcdev->is_interlaced) {
  414. height /= 2;
  415. cdwdr_width *= 2;
  416. }
  417. ceu_write(pcdev, CAMOR, 0);
  418. ceu_write(pcdev, CAPWR, (height << 16) | width);
  419. ceu_write(pcdev, CFLCR, 0); /* no scaling */
  420. ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
  421. ceu_write(pcdev, CLFCR, 0); /* no lowpass filter */
  422. /* A few words about byte order (observed in Big Endian mode)
  423. *
  424. * In data fetch mode bytes are received in chunks of 8 bytes.
  425. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  426. *
  427. * The data is however by default written to memory in reverse order:
  428. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  429. *
  430. * The lowest three bits of CDOCR allows us to do swapping,
  431. * using 7 we swap the data bytes to match the incoming order:
  432. * D0, D1, D2, D3, D4, D5, D6, D7
  433. */
  434. value = 0x00000017;
  435. if (yuv_lineskip)
  436. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  437. ceu_write(pcdev, CDOCR, value);
  438. ceu_write(pcdev, CDWDR, cdwdr_width);
  439. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  440. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  441. return 0;
  442. }
  443. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  444. {
  445. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  446. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  447. unsigned long camera_flags, common_flags;
  448. camera_flags = icd->ops->query_bus_param(icd);
  449. common_flags = soc_camera_bus_param_compatible(camera_flags,
  450. make_bus_param(pcdev));
  451. if (!common_flags)
  452. return -EINVAL;
  453. return 0;
  454. }
  455. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  456. {
  457. .name = "NV12",
  458. .depth = 12,
  459. .fourcc = V4L2_PIX_FMT_NV12,
  460. .colorspace = V4L2_COLORSPACE_JPEG,
  461. },
  462. {
  463. .name = "NV21",
  464. .depth = 12,
  465. .fourcc = V4L2_PIX_FMT_NV21,
  466. .colorspace = V4L2_COLORSPACE_JPEG,
  467. },
  468. {
  469. .name = "NV16",
  470. .depth = 16,
  471. .fourcc = V4L2_PIX_FMT_NV16,
  472. .colorspace = V4L2_COLORSPACE_JPEG,
  473. },
  474. {
  475. .name = "NV61",
  476. .depth = 16,
  477. .fourcc = V4L2_PIX_FMT_NV61,
  478. .colorspace = V4L2_COLORSPACE_JPEG,
  479. },
  480. };
  481. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  482. struct soc_camera_format_xlate *xlate)
  483. {
  484. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  485. int ret, k, n;
  486. int formats = 0;
  487. ret = sh_mobile_ceu_try_bus_param(icd);
  488. if (ret < 0)
  489. return 0;
  490. /* Beginning of a pass */
  491. if (!idx)
  492. icd->host_priv = NULL;
  493. switch (icd->formats[idx].fourcc) {
  494. case V4L2_PIX_FMT_UYVY:
  495. case V4L2_PIX_FMT_VYUY:
  496. case V4L2_PIX_FMT_YUYV:
  497. case V4L2_PIX_FMT_YVYU:
  498. if (icd->host_priv)
  499. goto add_single_format;
  500. /*
  501. * Our case is simple so far: for any of the above four camera
  502. * formats we add all our four synthesized NV* formats, so,
  503. * just marking the device with a single flag suffices. If
  504. * the format generation rules are more complex, you would have
  505. * to actually hang your already added / counted formats onto
  506. * the host_priv pointer and check whether the format you're
  507. * going to add now is already there.
  508. */
  509. icd->host_priv = (void *)sh_mobile_ceu_formats;
  510. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  511. formats += n;
  512. for (k = 0; xlate && k < n; k++) {
  513. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  514. xlate->cam_fmt = icd->formats + idx;
  515. xlate->buswidth = icd->formats[idx].depth;
  516. xlate++;
  517. dev_dbg(&ici->dev, "Providing format %s using %s\n",
  518. sh_mobile_ceu_formats[k].name,
  519. icd->formats[idx].name);
  520. }
  521. default:
  522. add_single_format:
  523. /* Generic pass-through */
  524. formats++;
  525. if (xlate) {
  526. xlate->host_fmt = icd->formats + idx;
  527. xlate->cam_fmt = icd->formats + idx;
  528. xlate->buswidth = icd->formats[idx].depth;
  529. xlate++;
  530. dev_dbg(&ici->dev,
  531. "Providing format %s in pass-through mode\n",
  532. icd->formats[idx].name);
  533. }
  534. }
  535. return formats;
  536. }
  537. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  538. struct v4l2_rect *rect)
  539. {
  540. return icd->ops->set_crop(icd, rect);
  541. }
  542. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  543. struct v4l2_format *f)
  544. {
  545. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  546. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  547. __u32 pixfmt = f->fmt.pix.pixelformat;
  548. const struct soc_camera_format_xlate *xlate;
  549. struct v4l2_format cam_f = *f;
  550. int ret;
  551. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  552. if (!xlate) {
  553. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  554. return -EINVAL;
  555. }
  556. cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
  557. ret = icd->ops->set_fmt(icd, &cam_f);
  558. if (!ret) {
  559. icd->buswidth = xlate->buswidth;
  560. icd->current_fmt = xlate->host_fmt;
  561. pcdev->camera_fmt = xlate->cam_fmt;
  562. }
  563. return ret;
  564. }
  565. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  566. struct v4l2_format *f)
  567. {
  568. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  569. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  570. const struct soc_camera_format_xlate *xlate;
  571. __u32 pixfmt = f->fmt.pix.pixelformat;
  572. int ret;
  573. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  574. if (!xlate) {
  575. dev_warn(&ici->dev, "Format %x not found\n", pixfmt);
  576. return -EINVAL;
  577. }
  578. /* FIXME: calculate using depth and bus width */
  579. if (f->fmt.pix.height < 4)
  580. f->fmt.pix.height = 4;
  581. if (f->fmt.pix.height > 1920)
  582. f->fmt.pix.height = 1920;
  583. if (f->fmt.pix.width < 2)
  584. f->fmt.pix.width = 2;
  585. if (f->fmt.pix.width > 2560)
  586. f->fmt.pix.width = 2560;
  587. f->fmt.pix.width &= ~0x01;
  588. f->fmt.pix.height &= ~0x03;
  589. f->fmt.pix.bytesperline = f->fmt.pix.width *
  590. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  591. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  592. /* limit to sensor capabilities */
  593. ret = icd->ops->try_fmt(icd, f);
  594. if (ret < 0)
  595. return ret;
  596. switch (f->fmt.pix.field) {
  597. case V4L2_FIELD_INTERLACED:
  598. pcdev->is_interlaced = 1;
  599. break;
  600. case V4L2_FIELD_ANY:
  601. f->fmt.pix.field = V4L2_FIELD_NONE;
  602. /* fall-through */
  603. case V4L2_FIELD_NONE:
  604. pcdev->is_interlaced = 0;
  605. break;
  606. default:
  607. ret = -EINVAL;
  608. break;
  609. }
  610. return ret;
  611. }
  612. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  613. struct v4l2_requestbuffers *p)
  614. {
  615. int i;
  616. /* This is for locking debugging only. I removed spinlocks and now I
  617. * check whether .prepare is ever called on a linked buffer, or whether
  618. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  619. * it hadn't triggered */
  620. for (i = 0; i < p->count; i++) {
  621. struct sh_mobile_ceu_buffer *buf;
  622. buf = container_of(icf->vb_vidq.bufs[i],
  623. struct sh_mobile_ceu_buffer, vb);
  624. INIT_LIST_HEAD(&buf->vb.queue);
  625. }
  626. return 0;
  627. }
  628. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  629. {
  630. struct soc_camera_file *icf = file->private_data;
  631. struct sh_mobile_ceu_buffer *buf;
  632. buf = list_entry(icf->vb_vidq.stream.next,
  633. struct sh_mobile_ceu_buffer, vb.stream);
  634. poll_wait(file, &buf->vb.done, pt);
  635. if (buf->vb.state == VIDEOBUF_DONE ||
  636. buf->vb.state == VIDEOBUF_ERROR)
  637. return POLLIN|POLLRDNORM;
  638. return 0;
  639. }
  640. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  641. struct v4l2_capability *cap)
  642. {
  643. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  644. cap->version = KERNEL_VERSION(0, 0, 5);
  645. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  646. return 0;
  647. }
  648. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  649. struct soc_camera_device *icd)
  650. {
  651. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  652. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  653. videobuf_queue_dma_contig_init(q,
  654. &sh_mobile_ceu_videobuf_ops,
  655. &ici->dev, &pcdev->lock,
  656. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  657. pcdev->is_interlaced ?
  658. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  659. sizeof(struct sh_mobile_ceu_buffer),
  660. icd);
  661. }
  662. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  663. .owner = THIS_MODULE,
  664. .add = sh_mobile_ceu_add_device,
  665. .remove = sh_mobile_ceu_remove_device,
  666. .get_formats = sh_mobile_ceu_get_formats,
  667. .set_crop = sh_mobile_ceu_set_crop,
  668. .set_fmt = sh_mobile_ceu_set_fmt,
  669. .try_fmt = sh_mobile_ceu_try_fmt,
  670. .reqbufs = sh_mobile_ceu_reqbufs,
  671. .poll = sh_mobile_ceu_poll,
  672. .querycap = sh_mobile_ceu_querycap,
  673. .set_bus_param = sh_mobile_ceu_set_bus_param,
  674. .init_videobuf = sh_mobile_ceu_init_videobuf,
  675. };
  676. static int sh_mobile_ceu_probe(struct platform_device *pdev)
  677. {
  678. struct sh_mobile_ceu_dev *pcdev;
  679. struct resource *res;
  680. void __iomem *base;
  681. char clk_name[8];
  682. unsigned int irq;
  683. int err = 0;
  684. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. irq = platform_get_irq(pdev, 0);
  686. if (!res || !irq) {
  687. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  688. err = -ENODEV;
  689. goto exit;
  690. }
  691. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  692. if (!pcdev) {
  693. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  694. err = -ENOMEM;
  695. goto exit;
  696. }
  697. platform_set_drvdata(pdev, pcdev);
  698. INIT_LIST_HEAD(&pcdev->capture);
  699. spin_lock_init(&pcdev->lock);
  700. pcdev->pdata = pdev->dev.platform_data;
  701. if (!pcdev->pdata) {
  702. err = -EINVAL;
  703. dev_err(&pdev->dev, "CEU platform data not set.\n");
  704. goto exit_kfree;
  705. }
  706. base = ioremap_nocache(res->start, res->end - res->start + 1);
  707. if (!base) {
  708. err = -ENXIO;
  709. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  710. goto exit_kfree;
  711. }
  712. pcdev->irq = irq;
  713. pcdev->base = base;
  714. pcdev->video_limit = 0; /* only enabled if second resource exists */
  715. pcdev->dev = &pdev->dev;
  716. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  717. if (res) {
  718. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  719. res->start,
  720. (res->end - res->start) + 1,
  721. DMA_MEMORY_MAP |
  722. DMA_MEMORY_EXCLUSIVE);
  723. if (!err) {
  724. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  725. err = -ENXIO;
  726. goto exit_iounmap;
  727. }
  728. pcdev->video_limit = (res->end - res->start) + 1;
  729. }
  730. /* request irq */
  731. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  732. dev_name(&pdev->dev), pcdev);
  733. if (err) {
  734. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  735. goto exit_release_mem;
  736. }
  737. snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
  738. pcdev->clk = clk_get(&pdev->dev, clk_name);
  739. if (IS_ERR(pcdev->clk)) {
  740. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  741. err = PTR_ERR(pcdev->clk);
  742. goto exit_free_irq;
  743. }
  744. pcdev->ici.priv = pcdev;
  745. pcdev->ici.dev.parent = &pdev->dev;
  746. pcdev->ici.nr = pdev->id;
  747. pcdev->ici.drv_name = dev_name(&pdev->dev);
  748. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  749. err = soc_camera_host_register(&pcdev->ici);
  750. if (err)
  751. goto exit_free_clk;
  752. return 0;
  753. exit_free_clk:
  754. clk_put(pcdev->clk);
  755. exit_free_irq:
  756. free_irq(pcdev->irq, pcdev);
  757. exit_release_mem:
  758. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  759. dma_release_declared_memory(&pdev->dev);
  760. exit_iounmap:
  761. iounmap(base);
  762. exit_kfree:
  763. kfree(pcdev);
  764. exit:
  765. return err;
  766. }
  767. static int sh_mobile_ceu_remove(struct platform_device *pdev)
  768. {
  769. struct sh_mobile_ceu_dev *pcdev = platform_get_drvdata(pdev);
  770. soc_camera_host_unregister(&pcdev->ici);
  771. clk_put(pcdev->clk);
  772. free_irq(pcdev->irq, pcdev);
  773. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  774. dma_release_declared_memory(&pdev->dev);
  775. iounmap(pcdev->base);
  776. kfree(pcdev);
  777. return 0;
  778. }
  779. static struct platform_driver sh_mobile_ceu_driver = {
  780. .driver = {
  781. .name = "sh_mobile_ceu",
  782. },
  783. .probe = sh_mobile_ceu_probe,
  784. .remove = sh_mobile_ceu_remove,
  785. };
  786. static int __init sh_mobile_ceu_init(void)
  787. {
  788. return platform_driver_register(&sh_mobile_ceu_driver);
  789. }
  790. static void __exit sh_mobile_ceu_exit(void)
  791. {
  792. platform_driver_unregister(&sh_mobile_ceu_driver);
  793. }
  794. module_init(sh_mobile_ceu_init);
  795. module_exit(sh_mobile_ceu_exit);
  796. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  797. MODULE_AUTHOR("Magnus Damm");
  798. MODULE_LICENSE("GPL");