sh_mobile_ceu_camera.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  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. 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. list_add_tail(&vb->queue, &pcdev->capture);
  249. if (!pcdev->active) {
  250. pcdev->active = vb;
  251. sh_mobile_ceu_capture(pcdev);
  252. }
  253. }
  254. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  255. struct videobuf_buffer *vb)
  256. {
  257. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  258. }
  259. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  260. .buf_setup = sh_mobile_ceu_videobuf_setup,
  261. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  262. .buf_queue = sh_mobile_ceu_videobuf_queue,
  263. .buf_release = sh_mobile_ceu_videobuf_release,
  264. };
  265. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  266. {
  267. struct sh_mobile_ceu_dev *pcdev = data;
  268. struct videobuf_buffer *vb;
  269. unsigned long flags;
  270. spin_lock_irqsave(&pcdev->lock, flags);
  271. vb = pcdev->active;
  272. list_del_init(&vb->queue);
  273. if (!list_empty(&pcdev->capture))
  274. pcdev->active = list_entry(pcdev->capture.next,
  275. struct videobuf_buffer, queue);
  276. else
  277. pcdev->active = NULL;
  278. sh_mobile_ceu_capture(pcdev);
  279. vb->state = VIDEOBUF_DONE;
  280. do_gettimeofday(&vb->ts);
  281. vb->field_count++;
  282. wake_up(&vb->done);
  283. spin_unlock_irqrestore(&pcdev->lock, flags);
  284. return IRQ_HANDLED;
  285. }
  286. /* Called with .video_lock held */
  287. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  288. {
  289. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  290. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  291. int ret = -EBUSY;
  292. if (pcdev->icd)
  293. goto err;
  294. dev_info(&icd->dev,
  295. "SuperH Mobile CEU driver attached to camera %d\n",
  296. icd->devnum);
  297. ret = icd->ops->init(icd);
  298. if (ret)
  299. goto err;
  300. clk_enable(pcdev->clk);
  301. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  302. while (ceu_read(pcdev, CSTSR) & 1)
  303. msleep(1);
  304. pcdev->icd = icd;
  305. err:
  306. return ret;
  307. }
  308. /* Called with .video_lock held */
  309. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  310. {
  311. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  312. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  313. unsigned long flags;
  314. BUG_ON(icd != pcdev->icd);
  315. /* disable capture, disable interrupts */
  316. ceu_write(pcdev, CEIER, 0);
  317. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  318. /* make sure active buffer is canceled */
  319. spin_lock_irqsave(&pcdev->lock, flags);
  320. if (pcdev->active) {
  321. list_del(&pcdev->active->queue);
  322. pcdev->active->state = VIDEOBUF_ERROR;
  323. wake_up_all(&pcdev->active->done);
  324. pcdev->active = NULL;
  325. }
  326. spin_unlock_irqrestore(&pcdev->lock, flags);
  327. clk_disable(pcdev->clk);
  328. icd->ops->release(icd);
  329. dev_info(&icd->dev,
  330. "SuperH Mobile CEU driver detached from camera %d\n",
  331. icd->devnum);
  332. pcdev->icd = NULL;
  333. }
  334. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  335. __u32 pixfmt)
  336. {
  337. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  338. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  339. int ret, buswidth, width, height, cfszr_width, cdwdr_width;
  340. unsigned long camera_flags, common_flags, value;
  341. int yuv_mode, yuv_lineskip;
  342. camera_flags = icd->ops->query_bus_param(icd);
  343. common_flags = soc_camera_bus_param_compatible(camera_flags,
  344. make_bus_param(pcdev));
  345. if (!common_flags)
  346. return -EINVAL;
  347. ret = icd->ops->set_bus_param(icd, common_flags);
  348. if (ret < 0)
  349. return ret;
  350. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  351. case SOCAM_DATAWIDTH_8:
  352. buswidth = 8;
  353. break;
  354. case SOCAM_DATAWIDTH_16:
  355. buswidth = 16;
  356. break;
  357. default:
  358. return -EINVAL;
  359. }
  360. ceu_write(pcdev, CRCNTR, 0);
  361. ceu_write(pcdev, CRCMPR, 0);
  362. value = 0x00000010; /* data fetch by default */
  363. yuv_mode = yuv_lineskip = 0;
  364. switch (icd->current_fmt->fourcc) {
  365. case V4L2_PIX_FMT_NV12:
  366. case V4L2_PIX_FMT_NV21:
  367. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  368. /* fall-through */
  369. case V4L2_PIX_FMT_NV16:
  370. case V4L2_PIX_FMT_NV61:
  371. yuv_mode = 1;
  372. switch (pcdev->camera_fmt->fourcc) {
  373. case V4L2_PIX_FMT_UYVY:
  374. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  375. break;
  376. case V4L2_PIX_FMT_VYUY:
  377. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  378. break;
  379. case V4L2_PIX_FMT_YUYV:
  380. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  381. break;
  382. case V4L2_PIX_FMT_YVYU:
  383. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  384. break;
  385. default:
  386. BUG();
  387. }
  388. }
  389. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  390. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  391. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  392. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  393. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  394. value |= buswidth == 16 ? 1 << 12 : 0;
  395. ceu_write(pcdev, CAMCR, value);
  396. ceu_write(pcdev, CAPCR, 0x00300000);
  397. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  398. mdelay(1);
  399. if (yuv_mode) {
  400. width = icd->width * 2;
  401. width = buswidth == 16 ? width / 2 : width;
  402. cfszr_width = cdwdr_width = icd->width;
  403. } else {
  404. width = icd->width * ((icd->current_fmt->depth + 7) >> 3);
  405. width = buswidth == 16 ? width / 2 : width;
  406. cfszr_width = buswidth == 8 ? width / 2 : width;
  407. cdwdr_width = buswidth == 16 ? width * 2 : width;
  408. }
  409. height = icd->height;
  410. if (pcdev->is_interlaced) {
  411. height /= 2;
  412. cdwdr_width *= 2;
  413. }
  414. ceu_write(pcdev, CAMOR, 0);
  415. ceu_write(pcdev, CAPWR, (height << 16) | width);
  416. ceu_write(pcdev, CFLCR, 0); /* no scaling */
  417. ceu_write(pcdev, CFSZR, (height << 16) | cfszr_width);
  418. ceu_write(pcdev, CLFCR, 0); /* no lowpass filter */
  419. /* A few words about byte order (observed in Big Endian mode)
  420. *
  421. * In data fetch mode bytes are received in chunks of 8 bytes.
  422. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  423. *
  424. * The data is however by default written to memory in reverse order:
  425. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  426. *
  427. * The lowest three bits of CDOCR allows us to do swapping,
  428. * using 7 we swap the data bytes to match the incoming order:
  429. * D0, D1, D2, D3, D4, D5, D6, D7
  430. */
  431. value = 0x00000017;
  432. if (yuv_lineskip)
  433. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  434. ceu_write(pcdev, CDOCR, value);
  435. ceu_write(pcdev, CDWDR, cdwdr_width);
  436. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  437. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  438. return 0;
  439. }
  440. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  441. {
  442. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  443. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  444. unsigned long camera_flags, common_flags;
  445. camera_flags = icd->ops->query_bus_param(icd);
  446. common_flags = soc_camera_bus_param_compatible(camera_flags,
  447. make_bus_param(pcdev));
  448. if (!common_flags)
  449. return -EINVAL;
  450. return 0;
  451. }
  452. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  453. {
  454. .name = "NV12",
  455. .depth = 12,
  456. .fourcc = V4L2_PIX_FMT_NV12,
  457. .colorspace = V4L2_COLORSPACE_JPEG,
  458. },
  459. {
  460. .name = "NV21",
  461. .depth = 12,
  462. .fourcc = V4L2_PIX_FMT_NV21,
  463. .colorspace = V4L2_COLORSPACE_JPEG,
  464. },
  465. {
  466. .name = "NV16",
  467. .depth = 16,
  468. .fourcc = V4L2_PIX_FMT_NV16,
  469. .colorspace = V4L2_COLORSPACE_JPEG,
  470. },
  471. {
  472. .name = "NV61",
  473. .depth = 16,
  474. .fourcc = V4L2_PIX_FMT_NV61,
  475. .colorspace = V4L2_COLORSPACE_JPEG,
  476. },
  477. };
  478. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  479. struct soc_camera_format_xlate *xlate)
  480. {
  481. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  482. int ret, k, n;
  483. int formats = 0;
  484. ret = sh_mobile_ceu_try_bus_param(icd);
  485. if (ret < 0)
  486. return 0;
  487. /* Beginning of a pass */
  488. if (!idx)
  489. icd->host_priv = NULL;
  490. switch (icd->formats[idx].fourcc) {
  491. case V4L2_PIX_FMT_UYVY:
  492. case V4L2_PIX_FMT_VYUY:
  493. case V4L2_PIX_FMT_YUYV:
  494. case V4L2_PIX_FMT_YVYU:
  495. if (icd->host_priv)
  496. goto add_single_format;
  497. /*
  498. * Our case is simple so far: for any of the above four camera
  499. * formats we add all our four synthesized NV* formats, so,
  500. * just marking the device with a single flag suffices. If
  501. * the format generation rules are more complex, you would have
  502. * to actually hang your already added / counted formats onto
  503. * the host_priv pointer and check whether the format you're
  504. * going to add now is already there.
  505. */
  506. icd->host_priv = (void *)sh_mobile_ceu_formats;
  507. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  508. formats += n;
  509. for (k = 0; xlate && k < n; k++) {
  510. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  511. xlate->cam_fmt = icd->formats + idx;
  512. xlate->buswidth = icd->formats[idx].depth;
  513. xlate++;
  514. dev_dbg(ici->dev, "Providing format %s using %s\n",
  515. sh_mobile_ceu_formats[k].name,
  516. icd->formats[idx].name);
  517. }
  518. default:
  519. add_single_format:
  520. /* Generic pass-through */
  521. formats++;
  522. if (xlate) {
  523. xlate->host_fmt = icd->formats + idx;
  524. xlate->cam_fmt = icd->formats + idx;
  525. xlate->buswidth = icd->formats[idx].depth;
  526. xlate++;
  527. dev_dbg(ici->dev,
  528. "Providing format %s in pass-through mode\n",
  529. icd->formats[idx].name);
  530. }
  531. }
  532. return formats;
  533. }
  534. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  535. struct v4l2_rect *rect)
  536. {
  537. return icd->ops->set_crop(icd, rect);
  538. }
  539. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  540. struct v4l2_format *f)
  541. {
  542. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  543. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  544. __u32 pixfmt = f->fmt.pix.pixelformat;
  545. const struct soc_camera_format_xlate *xlate;
  546. struct v4l2_format cam_f = *f;
  547. int ret;
  548. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  549. if (!xlate) {
  550. dev_warn(ici->dev, "Format %x not found\n", pixfmt);
  551. return -EINVAL;
  552. }
  553. cam_f.fmt.pix.pixelformat = xlate->cam_fmt->fourcc;
  554. ret = icd->ops->set_fmt(icd, &cam_f);
  555. if (!ret) {
  556. icd->buswidth = xlate->buswidth;
  557. icd->current_fmt = xlate->host_fmt;
  558. pcdev->camera_fmt = xlate->cam_fmt;
  559. }
  560. return ret;
  561. }
  562. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  563. struct v4l2_format *f)
  564. {
  565. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  566. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  567. const struct soc_camera_format_xlate *xlate;
  568. __u32 pixfmt = f->fmt.pix.pixelformat;
  569. int ret;
  570. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  571. if (!xlate) {
  572. dev_warn(ici->dev, "Format %x not found\n", pixfmt);
  573. return -EINVAL;
  574. }
  575. /* FIXME: calculate using depth and bus width */
  576. v4l_bound_align_image(&f->fmt.pix.width, 2, 2560, 1,
  577. &f->fmt.pix.height, 4, 1920, 2, 0);
  578. f->fmt.pix.bytesperline = f->fmt.pix.width *
  579. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  580. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  581. /* limit to sensor capabilities */
  582. ret = icd->ops->try_fmt(icd, f);
  583. if (ret < 0)
  584. return ret;
  585. switch (f->fmt.pix.field) {
  586. case V4L2_FIELD_INTERLACED:
  587. pcdev->is_interlaced = 1;
  588. break;
  589. case V4L2_FIELD_ANY:
  590. f->fmt.pix.field = V4L2_FIELD_NONE;
  591. /* fall-through */
  592. case V4L2_FIELD_NONE:
  593. pcdev->is_interlaced = 0;
  594. break;
  595. default:
  596. ret = -EINVAL;
  597. break;
  598. }
  599. return ret;
  600. }
  601. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  602. struct v4l2_requestbuffers *p)
  603. {
  604. int i;
  605. /* This is for locking debugging only. I removed spinlocks and now I
  606. * check whether .prepare is ever called on a linked buffer, or whether
  607. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  608. * it hadn't triggered */
  609. for (i = 0; i < p->count; i++) {
  610. struct sh_mobile_ceu_buffer *buf;
  611. buf = container_of(icf->vb_vidq.bufs[i],
  612. struct sh_mobile_ceu_buffer, vb);
  613. INIT_LIST_HEAD(&buf->vb.queue);
  614. }
  615. return 0;
  616. }
  617. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  618. {
  619. struct soc_camera_file *icf = file->private_data;
  620. struct sh_mobile_ceu_buffer *buf;
  621. buf = list_entry(icf->vb_vidq.stream.next,
  622. struct sh_mobile_ceu_buffer, vb.stream);
  623. poll_wait(file, &buf->vb.done, pt);
  624. if (buf->vb.state == VIDEOBUF_DONE ||
  625. buf->vb.state == VIDEOBUF_ERROR)
  626. return POLLIN|POLLRDNORM;
  627. return 0;
  628. }
  629. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  630. struct v4l2_capability *cap)
  631. {
  632. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  633. cap->version = KERNEL_VERSION(0, 0, 5);
  634. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  635. return 0;
  636. }
  637. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  638. struct soc_camera_device *icd)
  639. {
  640. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  641. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  642. videobuf_queue_dma_contig_init(q,
  643. &sh_mobile_ceu_videobuf_ops,
  644. ici->dev, &pcdev->lock,
  645. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  646. pcdev->is_interlaced ?
  647. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  648. sizeof(struct sh_mobile_ceu_buffer),
  649. icd);
  650. }
  651. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  652. .owner = THIS_MODULE,
  653. .add = sh_mobile_ceu_add_device,
  654. .remove = sh_mobile_ceu_remove_device,
  655. .get_formats = sh_mobile_ceu_get_formats,
  656. .set_crop = sh_mobile_ceu_set_crop,
  657. .set_fmt = sh_mobile_ceu_set_fmt,
  658. .try_fmt = sh_mobile_ceu_try_fmt,
  659. .reqbufs = sh_mobile_ceu_reqbufs,
  660. .poll = sh_mobile_ceu_poll,
  661. .querycap = sh_mobile_ceu_querycap,
  662. .set_bus_param = sh_mobile_ceu_set_bus_param,
  663. .init_videobuf = sh_mobile_ceu_init_videobuf,
  664. };
  665. static int sh_mobile_ceu_probe(struct platform_device *pdev)
  666. {
  667. struct sh_mobile_ceu_dev *pcdev;
  668. struct resource *res;
  669. void __iomem *base;
  670. char clk_name[8];
  671. unsigned int irq;
  672. int err = 0;
  673. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  674. irq = platform_get_irq(pdev, 0);
  675. if (!res || !irq) {
  676. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  677. err = -ENODEV;
  678. goto exit;
  679. }
  680. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  681. if (!pcdev) {
  682. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  683. err = -ENOMEM;
  684. goto exit;
  685. }
  686. INIT_LIST_HEAD(&pcdev->capture);
  687. spin_lock_init(&pcdev->lock);
  688. pcdev->pdata = pdev->dev.platform_data;
  689. if (!pcdev->pdata) {
  690. err = -EINVAL;
  691. dev_err(&pdev->dev, "CEU platform data not set.\n");
  692. goto exit_kfree;
  693. }
  694. base = ioremap_nocache(res->start, resource_size(res));
  695. if (!base) {
  696. err = -ENXIO;
  697. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  698. goto exit_kfree;
  699. }
  700. pcdev->irq = irq;
  701. pcdev->base = base;
  702. pcdev->video_limit = 0; /* only enabled if second resource exists */
  703. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  704. if (res) {
  705. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  706. res->start,
  707. resource_size(res),
  708. DMA_MEMORY_MAP |
  709. DMA_MEMORY_EXCLUSIVE);
  710. if (!err) {
  711. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  712. err = -ENXIO;
  713. goto exit_iounmap;
  714. }
  715. pcdev->video_limit = resource_size(res);
  716. }
  717. /* request irq */
  718. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  719. dev_name(&pdev->dev), pcdev);
  720. if (err) {
  721. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  722. goto exit_release_mem;
  723. }
  724. snprintf(clk_name, sizeof(clk_name), "ceu%d", pdev->id);
  725. pcdev->clk = clk_get(&pdev->dev, clk_name);
  726. if (IS_ERR(pcdev->clk)) {
  727. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  728. err = PTR_ERR(pcdev->clk);
  729. goto exit_free_irq;
  730. }
  731. pcdev->ici.priv = pcdev;
  732. pcdev->ici.dev = &pdev->dev;
  733. pcdev->ici.nr = pdev->id;
  734. pcdev->ici.drv_name = dev_name(&pdev->dev);
  735. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  736. err = soc_camera_host_register(&pcdev->ici);
  737. if (err)
  738. goto exit_free_clk;
  739. return 0;
  740. exit_free_clk:
  741. clk_put(pcdev->clk);
  742. exit_free_irq:
  743. free_irq(pcdev->irq, pcdev);
  744. exit_release_mem:
  745. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  746. dma_release_declared_memory(&pdev->dev);
  747. exit_iounmap:
  748. iounmap(base);
  749. exit_kfree:
  750. kfree(pcdev);
  751. exit:
  752. return err;
  753. }
  754. static int sh_mobile_ceu_remove(struct platform_device *pdev)
  755. {
  756. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  757. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  758. struct sh_mobile_ceu_dev, ici);
  759. soc_camera_host_unregister(soc_host);
  760. clk_put(pcdev->clk);
  761. free_irq(pcdev->irq, pcdev);
  762. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  763. dma_release_declared_memory(&pdev->dev);
  764. iounmap(pcdev->base);
  765. kfree(pcdev);
  766. return 0;
  767. }
  768. static struct platform_driver sh_mobile_ceu_driver = {
  769. .driver = {
  770. .name = "sh_mobile_ceu",
  771. },
  772. .probe = sh_mobile_ceu_probe,
  773. .remove = sh_mobile_ceu_remove,
  774. };
  775. static int __init sh_mobile_ceu_init(void)
  776. {
  777. return platform_driver_register(&sh_mobile_ceu_driver);
  778. }
  779. static void __exit sh_mobile_ceu_exit(void)
  780. {
  781. platform_driver_unregister(&sh_mobile_ceu_driver);
  782. }
  783. module_init(sh_mobile_ceu_init);
  784. module_exit(sh_mobile_ceu_exit);
  785. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  786. MODULE_AUTHOR("Magnus Damm");
  787. MODULE_LICENSE("GPL");