sh_mobile_ceu_camera.c 24 KB

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