sh_mobile_ceu_camera.c 25 KB

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