sh_mobile_ceu_camera.c 25 KB

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