sh_mobile_ceu_camera.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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 <linux/sched.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-dev.h>
  36. #include <media/soc_camera.h>
  37. #include <media/sh_mobile_ceu.h>
  38. #include <media/videobuf-dma-contig.h>
  39. /* register offsets for sh7722 / sh7723 */
  40. #define CAPSR 0x00 /* Capture start register */
  41. #define CAPCR 0x04 /* Capture control register */
  42. #define CAMCR 0x08 /* Capture interface control register */
  43. #define CMCYR 0x0c /* Capture interface cycle register */
  44. #define CAMOR 0x10 /* Capture interface offset register */
  45. #define CAPWR 0x14 /* Capture interface width register */
  46. #define CAIFR 0x18 /* Capture interface input format register */
  47. #define CSTCR 0x20 /* Camera strobe control register (<= sh7722) */
  48. #define CSECR 0x24 /* Camera strobe emission count register (<= sh7722) */
  49. #define CRCNTR 0x28 /* CEU register control register */
  50. #define CRCMPR 0x2c /* CEU register forcible control register */
  51. #define CFLCR 0x30 /* Capture filter control register */
  52. #define CFSZR 0x34 /* Capture filter size clip register */
  53. #define CDWDR 0x38 /* Capture destination width register */
  54. #define CDAYR 0x3c /* Capture data address Y register */
  55. #define CDACR 0x40 /* Capture data address C register */
  56. #define CDBYR 0x44 /* Capture data bottom-field address Y register */
  57. #define CDBCR 0x48 /* Capture data bottom-field address C register */
  58. #define CBDSR 0x4c /* Capture bundle destination size register */
  59. #define CFWCR 0x5c /* Firewall operation control register */
  60. #define CLFCR 0x60 /* Capture low-pass filter control register */
  61. #define CDOCR 0x64 /* Capture data output control register */
  62. #define CDDCR 0x68 /* Capture data complexity level register */
  63. #define CDDAR 0x6c /* Capture data complexity level address register */
  64. #define CEIER 0x70 /* Capture event interrupt enable register */
  65. #define CETCR 0x74 /* Capture event flag clear register */
  66. #define CSTSR 0x7c /* Capture status register */
  67. #define CSRTR 0x80 /* Capture software reset register */
  68. #define CDSSR 0x84 /* Capture data size register */
  69. #define CDAYR2 0x90 /* Capture data address Y register 2 */
  70. #define CDACR2 0x94 /* Capture data address C register 2 */
  71. #define CDBYR2 0x98 /* Capture data bottom-field address Y register 2 */
  72. #define CDBCR2 0x9c /* Capture data bottom-field address C register 2 */
  73. #undef DEBUG_GEOMETRY
  74. #ifdef DEBUG_GEOMETRY
  75. #define dev_geo dev_info
  76. #else
  77. #define dev_geo dev_dbg
  78. #endif
  79. /* per video frame buffer */
  80. struct sh_mobile_ceu_buffer {
  81. struct videobuf_buffer vb; /* v4l buffer must be first */
  82. const struct soc_camera_data_format *fmt;
  83. };
  84. struct sh_mobile_ceu_dev {
  85. struct soc_camera_host ici;
  86. struct soc_camera_device *icd;
  87. unsigned int irq;
  88. void __iomem *base;
  89. unsigned long video_limit;
  90. /* lock used to protect videobuf */
  91. spinlock_t lock;
  92. struct list_head capture;
  93. struct videobuf_buffer *active;
  94. struct sh_mobile_ceu_info *pdata;
  95. u32 cflcr;
  96. unsigned int is_interlaced:1;
  97. unsigned int image_mode:1;
  98. unsigned int is_16bit:1;
  99. };
  100. struct sh_mobile_ceu_cam {
  101. struct v4l2_rect ceu_rect;
  102. unsigned int cam_width;
  103. unsigned int cam_height;
  104. const struct soc_camera_data_format *extra_fmt;
  105. const struct soc_camera_data_format *camera_fmt;
  106. };
  107. static unsigned long make_bus_param(struct sh_mobile_ceu_dev *pcdev)
  108. {
  109. unsigned long flags;
  110. flags = SOCAM_MASTER |
  111. SOCAM_PCLK_SAMPLE_RISING |
  112. SOCAM_HSYNC_ACTIVE_HIGH |
  113. SOCAM_HSYNC_ACTIVE_LOW |
  114. SOCAM_VSYNC_ACTIVE_HIGH |
  115. SOCAM_VSYNC_ACTIVE_LOW |
  116. SOCAM_DATA_ACTIVE_HIGH;
  117. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_8BIT_BUS)
  118. flags |= SOCAM_DATAWIDTH_8;
  119. if (pcdev->pdata->flags & SH_CEU_FLAG_USE_16BIT_BUS)
  120. flags |= SOCAM_DATAWIDTH_16;
  121. if (flags & SOCAM_DATAWIDTH_MASK)
  122. return flags;
  123. return 0;
  124. }
  125. static void ceu_write(struct sh_mobile_ceu_dev *priv,
  126. unsigned long reg_offs, u32 data)
  127. {
  128. iowrite32(data, priv->base + reg_offs);
  129. }
  130. static u32 ceu_read(struct sh_mobile_ceu_dev *priv, unsigned long reg_offs)
  131. {
  132. return ioread32(priv->base + reg_offs);
  133. }
  134. static int sh_mobile_ceu_soft_reset(struct sh_mobile_ceu_dev *pcdev)
  135. {
  136. int i, success = 0;
  137. struct soc_camera_device *icd = pcdev->icd;
  138. ceu_write(pcdev, CAPSR, 1 << 16); /* reset */
  139. /* wait CSTSR.CPTON bit */
  140. for (i = 0; i < 1000; i++) {
  141. if (!(ceu_read(pcdev, CSTSR) & 1)) {
  142. success++;
  143. break;
  144. }
  145. udelay(1);
  146. }
  147. /* wait CAPSR.CPKIL bit */
  148. for (i = 0; i < 1000; i++) {
  149. if (!(ceu_read(pcdev, CAPSR) & (1 << 16))) {
  150. success++;
  151. break;
  152. }
  153. udelay(1);
  154. }
  155. if (2 != success) {
  156. dev_warn(&icd->dev, "soft reset time out\n");
  157. return -EIO;
  158. }
  159. return 0;
  160. }
  161. /*
  162. * Videobuf operations
  163. */
  164. static int sh_mobile_ceu_videobuf_setup(struct videobuf_queue *vq,
  165. unsigned int *count,
  166. unsigned int *size)
  167. {
  168. struct soc_camera_device *icd = vq->priv_data;
  169. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  170. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  171. int bytes_per_pixel = (icd->current_fmt->depth + 7) >> 3;
  172. *size = PAGE_ALIGN(icd->user_width * icd->user_height *
  173. bytes_per_pixel);
  174. if (0 == *count)
  175. *count = 2;
  176. if (pcdev->video_limit) {
  177. while (*size * *count > pcdev->video_limit)
  178. (*count)--;
  179. }
  180. dev_dbg(icd->dev.parent, "count=%d, size=%d\n", *count, *size);
  181. return 0;
  182. }
  183. static void free_buffer(struct videobuf_queue *vq,
  184. struct sh_mobile_ceu_buffer *buf)
  185. {
  186. struct soc_camera_device *icd = vq->priv_data;
  187. struct device *dev = icd->dev.parent;
  188. dev_dbg(dev, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  189. &buf->vb, buf->vb.baddr, buf->vb.bsize);
  190. if (in_interrupt())
  191. BUG();
  192. videobuf_waiton(&buf->vb, 0, 0);
  193. videobuf_dma_contig_free(vq, &buf->vb);
  194. dev_dbg(dev, "%s freed\n", __func__);
  195. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  196. }
  197. #define CEU_CETCR_MAGIC 0x0317f313 /* acknowledge magical interrupt sources */
  198. #define CEU_CETCR_IGRW (1 << 4) /* prohibited register access interrupt bit */
  199. #define CEU_CEIER_CPEIE (1 << 0) /* one-frame capture end interrupt */
  200. #define CEU_CEIER_VBP (1 << 20) /* vbp error */
  201. #define CEU_CAPCR_CTNCP (1 << 16) /* continuous capture mode (if set) */
  202. #define CEU_CEIER_MASK (CEU_CEIER_CPEIE | CEU_CEIER_VBP)
  203. /*
  204. * return value doesn't reflex the success/failure to queue the new buffer,
  205. * but rather the status of the previous buffer.
  206. */
  207. static int sh_mobile_ceu_capture(struct sh_mobile_ceu_dev *pcdev)
  208. {
  209. struct soc_camera_device *icd = pcdev->icd;
  210. dma_addr_t phys_addr_top, phys_addr_bottom;
  211. u32 status;
  212. int ret = 0;
  213. /*
  214. * The hardware is _very_ picky about this sequence. Especially
  215. * the CEU_CETCR_MAGIC value. It seems like we need to acknowledge
  216. * several not-so-well documented interrupt sources in CETCR.
  217. */
  218. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) & ~CEU_CEIER_MASK);
  219. status = ceu_read(pcdev, CETCR);
  220. ceu_write(pcdev, CETCR, ~status & CEU_CETCR_MAGIC);
  221. ceu_write(pcdev, CEIER, ceu_read(pcdev, CEIER) | CEU_CEIER_MASK);
  222. ceu_write(pcdev, CAPCR, ceu_read(pcdev, CAPCR) & ~CEU_CAPCR_CTNCP);
  223. ceu_write(pcdev, CETCR, CEU_CETCR_MAGIC ^ CEU_CETCR_IGRW);
  224. /*
  225. * When a VBP interrupt occurs, a capture end interrupt does not occur
  226. * and the image of that frame is not captured correctly. So, soft reset
  227. * is needed here.
  228. */
  229. if (status & CEU_CEIER_VBP) {
  230. sh_mobile_ceu_soft_reset(pcdev);
  231. ret = -EIO;
  232. }
  233. if (!pcdev->active)
  234. return ret;
  235. phys_addr_top = videobuf_to_dma_contig(pcdev->active);
  236. ceu_write(pcdev, CDAYR, phys_addr_top);
  237. if (pcdev->is_interlaced) {
  238. phys_addr_bottom = phys_addr_top + icd->user_width;
  239. ceu_write(pcdev, CDBYR, phys_addr_bottom);
  240. }
  241. switch (icd->current_fmt->fourcc) {
  242. case V4L2_PIX_FMT_NV12:
  243. case V4L2_PIX_FMT_NV21:
  244. case V4L2_PIX_FMT_NV16:
  245. case V4L2_PIX_FMT_NV61:
  246. phys_addr_top += icd->user_width *
  247. icd->user_height;
  248. ceu_write(pcdev, CDACR, phys_addr_top);
  249. if (pcdev->is_interlaced) {
  250. phys_addr_bottom = phys_addr_top +
  251. icd->user_width;
  252. ceu_write(pcdev, CDBCR, phys_addr_bottom);
  253. }
  254. }
  255. pcdev->active->state = VIDEOBUF_ACTIVE;
  256. ceu_write(pcdev, CAPSR, 0x1); /* start capture */
  257. return ret;
  258. }
  259. static int sh_mobile_ceu_videobuf_prepare(struct videobuf_queue *vq,
  260. struct videobuf_buffer *vb,
  261. enum v4l2_field field)
  262. {
  263. struct soc_camera_device *icd = vq->priv_data;
  264. struct sh_mobile_ceu_buffer *buf;
  265. int ret;
  266. buf = container_of(vb, struct sh_mobile_ceu_buffer, vb);
  267. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  268. vb, vb->baddr, vb->bsize);
  269. /* Added list head initialization on alloc */
  270. WARN_ON(!list_empty(&vb->queue));
  271. #ifdef DEBUG
  272. /*
  273. * This can be useful if you want to see if we actually fill
  274. * the buffer with something
  275. */
  276. memset((void *)vb->baddr, 0xaa, vb->bsize);
  277. #endif
  278. BUG_ON(NULL == icd->current_fmt);
  279. if (buf->fmt != icd->current_fmt ||
  280. vb->width != icd->user_width ||
  281. vb->height != icd->user_height ||
  282. vb->field != field) {
  283. buf->fmt = icd->current_fmt;
  284. vb->width = icd->user_width;
  285. vb->height = icd->user_height;
  286. vb->field = field;
  287. vb->state = VIDEOBUF_NEEDS_INIT;
  288. }
  289. vb->size = vb->width * vb->height * ((buf->fmt->depth + 7) >> 3);
  290. if (0 != vb->baddr && vb->bsize < vb->size) {
  291. ret = -EINVAL;
  292. goto out;
  293. }
  294. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  295. ret = videobuf_iolock(vq, vb, NULL);
  296. if (ret)
  297. goto fail;
  298. vb->state = VIDEOBUF_PREPARED;
  299. }
  300. return 0;
  301. fail:
  302. free_buffer(vq, buf);
  303. out:
  304. return ret;
  305. }
  306. /* Called under spinlock_irqsave(&pcdev->lock, ...) */
  307. static void sh_mobile_ceu_videobuf_queue(struct videobuf_queue *vq,
  308. struct videobuf_buffer *vb)
  309. {
  310. struct soc_camera_device *icd = vq->priv_data;
  311. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  312. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  313. dev_dbg(icd->dev.parent, "%s (vb=0x%p) 0x%08lx %zd\n", __func__,
  314. vb, vb->baddr, vb->bsize);
  315. vb->state = VIDEOBUF_QUEUED;
  316. list_add_tail(&vb->queue, &pcdev->capture);
  317. if (!pcdev->active) {
  318. /*
  319. * Because there were no active buffer at this moment,
  320. * we are not interested in the return value of
  321. * sh_mobile_ceu_capture here.
  322. */
  323. pcdev->active = vb;
  324. sh_mobile_ceu_capture(pcdev);
  325. }
  326. }
  327. static void sh_mobile_ceu_videobuf_release(struct videobuf_queue *vq,
  328. struct videobuf_buffer *vb)
  329. {
  330. struct soc_camera_device *icd = vq->priv_data;
  331. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  332. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  333. unsigned long flags;
  334. spin_lock_irqsave(&pcdev->lock, flags);
  335. if (pcdev->active == vb) {
  336. /* disable capture (release DMA buffer), reset */
  337. ceu_write(pcdev, CAPSR, 1 << 16);
  338. pcdev->active = NULL;
  339. }
  340. if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED) &&
  341. !list_empty(&vb->queue)) {
  342. vb->state = VIDEOBUF_ERROR;
  343. list_del_init(&vb->queue);
  344. }
  345. spin_unlock_irqrestore(&pcdev->lock, flags);
  346. free_buffer(vq, container_of(vb, struct sh_mobile_ceu_buffer, vb));
  347. }
  348. static struct videobuf_queue_ops sh_mobile_ceu_videobuf_ops = {
  349. .buf_setup = sh_mobile_ceu_videobuf_setup,
  350. .buf_prepare = sh_mobile_ceu_videobuf_prepare,
  351. .buf_queue = sh_mobile_ceu_videobuf_queue,
  352. .buf_release = sh_mobile_ceu_videobuf_release,
  353. };
  354. static irqreturn_t sh_mobile_ceu_irq(int irq, void *data)
  355. {
  356. struct sh_mobile_ceu_dev *pcdev = data;
  357. struct videobuf_buffer *vb;
  358. unsigned long flags;
  359. spin_lock_irqsave(&pcdev->lock, flags);
  360. vb = pcdev->active;
  361. if (!vb)
  362. /* Stale interrupt from a released buffer */
  363. goto out;
  364. list_del_init(&vb->queue);
  365. if (!list_empty(&pcdev->capture))
  366. pcdev->active = list_entry(pcdev->capture.next,
  367. struct videobuf_buffer, queue);
  368. else
  369. pcdev->active = NULL;
  370. vb->state = (sh_mobile_ceu_capture(pcdev) < 0) ?
  371. VIDEOBUF_ERROR : VIDEOBUF_DONE;
  372. do_gettimeofday(&vb->ts);
  373. vb->field_count++;
  374. wake_up(&vb->done);
  375. out:
  376. spin_unlock_irqrestore(&pcdev->lock, flags);
  377. return IRQ_HANDLED;
  378. }
  379. /* Called with .video_lock held */
  380. static int sh_mobile_ceu_add_device(struct soc_camera_device *icd)
  381. {
  382. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  383. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  384. int ret;
  385. if (pcdev->icd)
  386. return -EBUSY;
  387. dev_info(icd->dev.parent,
  388. "SuperH Mobile CEU driver attached to camera %d\n",
  389. icd->devnum);
  390. pm_runtime_get_sync(ici->v4l2_dev.dev);
  391. ret = sh_mobile_ceu_soft_reset(pcdev);
  392. if (!ret)
  393. pcdev->icd = icd;
  394. return ret;
  395. }
  396. /* Called with .video_lock held */
  397. static void sh_mobile_ceu_remove_device(struct soc_camera_device *icd)
  398. {
  399. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  400. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  401. unsigned long flags;
  402. BUG_ON(icd != pcdev->icd);
  403. /* disable capture, disable interrupts */
  404. ceu_write(pcdev, CEIER, 0);
  405. sh_mobile_ceu_soft_reset(pcdev);
  406. /* make sure active buffer is canceled */
  407. spin_lock_irqsave(&pcdev->lock, flags);
  408. if (pcdev->active) {
  409. list_del(&pcdev->active->queue);
  410. pcdev->active->state = VIDEOBUF_ERROR;
  411. wake_up_all(&pcdev->active->done);
  412. pcdev->active = NULL;
  413. }
  414. spin_unlock_irqrestore(&pcdev->lock, flags);
  415. pm_runtime_put_sync(ici->v4l2_dev.dev);
  416. dev_info(icd->dev.parent,
  417. "SuperH Mobile CEU driver detached from camera %d\n",
  418. icd->devnum);
  419. pcdev->icd = NULL;
  420. }
  421. /*
  422. * See chapter 29.4.12 "Capture Filter Control Register (CFLCR)"
  423. * in SH7722 Hardware Manual
  424. */
  425. static unsigned int size_dst(unsigned int src, unsigned int scale)
  426. {
  427. unsigned int mant_pre = scale >> 12;
  428. if (!src || !scale)
  429. return src;
  430. return ((mant_pre + 2 * (src - 1)) / (2 * mant_pre) - 1) *
  431. mant_pre * 4096 / scale + 1;
  432. }
  433. static u16 calc_scale(unsigned int src, unsigned int *dst)
  434. {
  435. u16 scale;
  436. if (src == *dst)
  437. return 0;
  438. scale = (src * 4096 / *dst) & ~7;
  439. while (scale > 4096 && size_dst(src, scale) < *dst)
  440. scale -= 8;
  441. *dst = size_dst(src, scale);
  442. return scale;
  443. }
  444. /* rect is guaranteed to not exceed the scaled camera rectangle */
  445. static void sh_mobile_ceu_set_rect(struct soc_camera_device *icd,
  446. unsigned int out_width,
  447. unsigned int out_height)
  448. {
  449. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  450. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  451. struct v4l2_rect *rect = &cam->ceu_rect;
  452. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  453. unsigned int height, width, cdwdr_width, in_width, in_height;
  454. unsigned int left_offset, top_offset;
  455. u32 camor;
  456. dev_dbg(icd->dev.parent, "Crop %ux%u@%u:%u\n",
  457. rect->width, rect->height, rect->left, rect->top);
  458. left_offset = rect->left;
  459. top_offset = rect->top;
  460. if (pcdev->image_mode) {
  461. in_width = rect->width;
  462. if (!pcdev->is_16bit) {
  463. in_width *= 2;
  464. left_offset *= 2;
  465. }
  466. width = cdwdr_width = out_width;
  467. } else {
  468. unsigned int w_factor = (icd->current_fmt->depth + 7) >> 3;
  469. width = out_width * w_factor / 2;
  470. if (!pcdev->is_16bit)
  471. w_factor *= 2;
  472. in_width = rect->width * w_factor / 2;
  473. left_offset = left_offset * w_factor / 2;
  474. cdwdr_width = width * 2;
  475. }
  476. height = out_height;
  477. in_height = rect->height;
  478. if (pcdev->is_interlaced) {
  479. height /= 2;
  480. in_height /= 2;
  481. top_offset /= 2;
  482. cdwdr_width *= 2;
  483. }
  484. /* Set CAMOR, CAPWR, CFSZR, take care of CDWDR */
  485. camor = left_offset | (top_offset << 16);
  486. dev_geo(icd->dev.parent,
  487. "CAMOR 0x%x, CAPWR 0x%x, CFSZR 0x%x, CDWDR 0x%x\n", camor,
  488. (in_height << 16) | in_width, (height << 16) | width,
  489. cdwdr_width);
  490. ceu_write(pcdev, CAMOR, camor);
  491. ceu_write(pcdev, CAPWR, (in_height << 16) | in_width);
  492. ceu_write(pcdev, CFSZR, (height << 16) | width);
  493. ceu_write(pcdev, CDWDR, cdwdr_width);
  494. }
  495. static u32 capture_save_reset(struct sh_mobile_ceu_dev *pcdev)
  496. {
  497. u32 capsr = ceu_read(pcdev, CAPSR);
  498. ceu_write(pcdev, CAPSR, 1 << 16); /* reset, stop capture */
  499. return capsr;
  500. }
  501. static void capture_restore(struct sh_mobile_ceu_dev *pcdev, u32 capsr)
  502. {
  503. unsigned long timeout = jiffies + 10 * HZ;
  504. /*
  505. * Wait until the end of the current frame. It can take a long time,
  506. * but if it has been aborted by a CAPSR reset, it shoule exit sooner.
  507. */
  508. while ((ceu_read(pcdev, CSTSR) & 1) && time_before(jiffies, timeout))
  509. msleep(1);
  510. if (time_after(jiffies, timeout)) {
  511. dev_err(pcdev->ici.v4l2_dev.dev,
  512. "Timeout waiting for frame end! Interface problem?\n");
  513. return;
  514. }
  515. /* Wait until reset clears, this shall not hang... */
  516. while (ceu_read(pcdev, CAPSR) & (1 << 16))
  517. udelay(10);
  518. /* Anything to restore? */
  519. if (capsr & ~(1 << 16))
  520. ceu_write(pcdev, CAPSR, capsr);
  521. }
  522. static int sh_mobile_ceu_set_bus_param(struct soc_camera_device *icd,
  523. __u32 pixfmt)
  524. {
  525. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  526. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  527. int ret;
  528. unsigned long camera_flags, common_flags, value;
  529. int yuv_lineskip;
  530. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  531. u32 capsr = capture_save_reset(pcdev);
  532. camera_flags = icd->ops->query_bus_param(icd);
  533. common_flags = soc_camera_bus_param_compatible(camera_flags,
  534. make_bus_param(pcdev));
  535. if (!common_flags)
  536. return -EINVAL;
  537. ret = icd->ops->set_bus_param(icd, common_flags);
  538. if (ret < 0)
  539. return ret;
  540. switch (common_flags & SOCAM_DATAWIDTH_MASK) {
  541. case SOCAM_DATAWIDTH_8:
  542. pcdev->is_16bit = 0;
  543. break;
  544. case SOCAM_DATAWIDTH_16:
  545. pcdev->is_16bit = 1;
  546. break;
  547. default:
  548. return -EINVAL;
  549. }
  550. ceu_write(pcdev, CRCNTR, 0);
  551. ceu_write(pcdev, CRCMPR, 0);
  552. value = 0x00000010; /* data fetch by default */
  553. yuv_lineskip = 0;
  554. switch (icd->current_fmt->fourcc) {
  555. case V4L2_PIX_FMT_NV12:
  556. case V4L2_PIX_FMT_NV21:
  557. yuv_lineskip = 1; /* skip for NV12/21, no skip for NV16/61 */
  558. /* fall-through */
  559. case V4L2_PIX_FMT_NV16:
  560. case V4L2_PIX_FMT_NV61:
  561. switch (cam->camera_fmt->fourcc) {
  562. case V4L2_PIX_FMT_UYVY:
  563. value = 0x00000000; /* Cb0, Y0, Cr0, Y1 */
  564. break;
  565. case V4L2_PIX_FMT_VYUY:
  566. value = 0x00000100; /* Cr0, Y0, Cb0, Y1 */
  567. break;
  568. case V4L2_PIX_FMT_YUYV:
  569. value = 0x00000200; /* Y0, Cb0, Y1, Cr0 */
  570. break;
  571. case V4L2_PIX_FMT_YVYU:
  572. value = 0x00000300; /* Y0, Cr0, Y1, Cb0 */
  573. break;
  574. default:
  575. BUG();
  576. }
  577. }
  578. if (icd->current_fmt->fourcc == V4L2_PIX_FMT_NV21 ||
  579. icd->current_fmt->fourcc == V4L2_PIX_FMT_NV61)
  580. value ^= 0x00000100; /* swap U, V to change from NV1x->NVx1 */
  581. value |= common_flags & SOCAM_VSYNC_ACTIVE_LOW ? 1 << 1 : 0;
  582. value |= common_flags & SOCAM_HSYNC_ACTIVE_LOW ? 1 << 0 : 0;
  583. value |= pcdev->is_16bit ? 1 << 12 : 0;
  584. ceu_write(pcdev, CAMCR, value);
  585. ceu_write(pcdev, CAPCR, 0x00300000);
  586. ceu_write(pcdev, CAIFR, pcdev->is_interlaced ? 0x101 : 0);
  587. sh_mobile_ceu_set_rect(icd, icd->user_width, icd->user_height);
  588. mdelay(1);
  589. ceu_write(pcdev, CFLCR, pcdev->cflcr);
  590. /*
  591. * A few words about byte order (observed in Big Endian mode)
  592. *
  593. * In data fetch mode bytes are received in chunks of 8 bytes.
  594. * D0, D1, D2, D3, D4, D5, D6, D7 (D0 received first)
  595. *
  596. * The data is however by default written to memory in reverse order:
  597. * D7, D6, D5, D4, D3, D2, D1, D0 (D7 written to lowest byte)
  598. *
  599. * The lowest three bits of CDOCR allows us to do swapping,
  600. * using 7 we swap the data bytes to match the incoming order:
  601. * D0, D1, D2, D3, D4, D5, D6, D7
  602. */
  603. value = 0x00000017;
  604. if (yuv_lineskip)
  605. value &= ~0x00000010; /* convert 4:2:2 -> 4:2:0 */
  606. ceu_write(pcdev, CDOCR, value);
  607. ceu_write(pcdev, CFWCR, 0); /* keep "datafetch firewall" disabled */
  608. dev_dbg(icd->dev.parent, "S_FMT successful for %c%c%c%c %ux%u\n",
  609. pixfmt & 0xff, (pixfmt >> 8) & 0xff,
  610. (pixfmt >> 16) & 0xff, (pixfmt >> 24) & 0xff,
  611. icd->user_width, icd->user_height);
  612. capture_restore(pcdev, capsr);
  613. /* not in bundle mode: skip CBDSR, CDAYR2, CDACR2, CDBYR2, CDBCR2 */
  614. return 0;
  615. }
  616. static int sh_mobile_ceu_try_bus_param(struct soc_camera_device *icd)
  617. {
  618. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  619. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  620. unsigned long camera_flags, common_flags;
  621. camera_flags = icd->ops->query_bus_param(icd);
  622. common_flags = soc_camera_bus_param_compatible(camera_flags,
  623. make_bus_param(pcdev));
  624. if (!common_flags)
  625. return -EINVAL;
  626. return 0;
  627. }
  628. static const struct soc_camera_data_format sh_mobile_ceu_formats[] = {
  629. {
  630. .name = "NV12",
  631. .depth = 12,
  632. .fourcc = V4L2_PIX_FMT_NV12,
  633. .colorspace = V4L2_COLORSPACE_JPEG,
  634. },
  635. {
  636. .name = "NV21",
  637. .depth = 12,
  638. .fourcc = V4L2_PIX_FMT_NV21,
  639. .colorspace = V4L2_COLORSPACE_JPEG,
  640. },
  641. {
  642. .name = "NV16",
  643. .depth = 16,
  644. .fourcc = V4L2_PIX_FMT_NV16,
  645. .colorspace = V4L2_COLORSPACE_JPEG,
  646. },
  647. {
  648. .name = "NV61",
  649. .depth = 16,
  650. .fourcc = V4L2_PIX_FMT_NV61,
  651. .colorspace = V4L2_COLORSPACE_JPEG,
  652. },
  653. };
  654. static int sh_mobile_ceu_get_formats(struct soc_camera_device *icd, int idx,
  655. struct soc_camera_format_xlate *xlate)
  656. {
  657. struct device *dev = icd->dev.parent;
  658. int ret, k, n;
  659. int formats = 0;
  660. struct sh_mobile_ceu_cam *cam;
  661. ret = sh_mobile_ceu_try_bus_param(icd);
  662. if (ret < 0)
  663. return 0;
  664. if (!icd->host_priv) {
  665. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  666. if (!cam)
  667. return -ENOMEM;
  668. icd->host_priv = cam;
  669. } else {
  670. cam = icd->host_priv;
  671. }
  672. /* Beginning of a pass */
  673. if (!idx)
  674. cam->extra_fmt = NULL;
  675. switch (icd->formats[idx].fourcc) {
  676. case V4L2_PIX_FMT_UYVY:
  677. case V4L2_PIX_FMT_VYUY:
  678. case V4L2_PIX_FMT_YUYV:
  679. case V4L2_PIX_FMT_YVYU:
  680. if (cam->extra_fmt)
  681. goto add_single_format;
  682. /*
  683. * Our case is simple so far: for any of the above four camera
  684. * formats we add all our four synthesized NV* formats, so,
  685. * just marking the device with a single flag suffices. If
  686. * the format generation rules are more complex, you would have
  687. * to actually hang your already added / counted formats onto
  688. * the host_priv pointer and check whether the format you're
  689. * going to add now is already there.
  690. */
  691. cam->extra_fmt = (void *)sh_mobile_ceu_formats;
  692. n = ARRAY_SIZE(sh_mobile_ceu_formats);
  693. formats += n;
  694. for (k = 0; xlate && k < n; k++) {
  695. xlate->host_fmt = &sh_mobile_ceu_formats[k];
  696. xlate->cam_fmt = icd->formats + idx;
  697. xlate->buswidth = icd->formats[idx].depth;
  698. xlate++;
  699. dev_dbg(dev, "Providing format %s using %s\n",
  700. sh_mobile_ceu_formats[k].name,
  701. icd->formats[idx].name);
  702. }
  703. default:
  704. add_single_format:
  705. /* Generic pass-through */
  706. formats++;
  707. if (xlate) {
  708. xlate->host_fmt = icd->formats + idx;
  709. xlate->cam_fmt = icd->formats + idx;
  710. xlate->buswidth = icd->formats[idx].depth;
  711. xlate++;
  712. dev_dbg(dev,
  713. "Providing format %s in pass-through mode\n",
  714. icd->formats[idx].name);
  715. }
  716. }
  717. return formats;
  718. }
  719. static void sh_mobile_ceu_put_formats(struct soc_camera_device *icd)
  720. {
  721. kfree(icd->host_priv);
  722. icd->host_priv = NULL;
  723. }
  724. /* Check if any dimension of r1 is smaller than respective one of r2 */
  725. static bool is_smaller(struct v4l2_rect *r1, struct v4l2_rect *r2)
  726. {
  727. return r1->width < r2->width || r1->height < r2->height;
  728. }
  729. /* Check if r1 fails to cover r2 */
  730. static bool is_inside(struct v4l2_rect *r1, struct v4l2_rect *r2)
  731. {
  732. return r1->left > r2->left || r1->top > r2->top ||
  733. r1->left + r1->width < r2->left + r2->width ||
  734. r1->top + r1->height < r2->top + r2->height;
  735. }
  736. static unsigned int scale_down(unsigned int size, unsigned int scale)
  737. {
  738. return (size * 4096 + scale / 2) / scale;
  739. }
  740. static unsigned int scale_up(unsigned int size, unsigned int scale)
  741. {
  742. return (size * scale + 2048) / 4096;
  743. }
  744. static unsigned int calc_generic_scale(unsigned int input, unsigned int output)
  745. {
  746. return (input * 4096 + output / 2) / output;
  747. }
  748. static int client_g_rect(struct v4l2_subdev *sd, struct v4l2_rect *rect)
  749. {
  750. struct v4l2_crop crop;
  751. struct v4l2_cropcap cap;
  752. int ret;
  753. crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  754. ret = v4l2_subdev_call(sd, video, g_crop, &crop);
  755. if (!ret) {
  756. *rect = crop.c;
  757. return ret;
  758. }
  759. /* Camera driver doesn't support .g_crop(), assume default rectangle */
  760. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  761. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  762. if (ret < 0)
  763. return ret;
  764. *rect = cap.defrect;
  765. return ret;
  766. }
  767. /*
  768. * The common for both scaling and cropping iterative approach is:
  769. * 1. try if the client can produce exactly what requested by the user
  770. * 2. if (1) failed, try to double the client image until we get one big enough
  771. * 3. if (2) failed, try to request the maximum image
  772. */
  773. static int client_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *crop,
  774. struct v4l2_crop *cam_crop)
  775. {
  776. struct v4l2_rect *rect = &crop->c, *cam_rect = &cam_crop->c;
  777. struct device *dev = sd->v4l2_dev->dev;
  778. struct v4l2_cropcap cap;
  779. int ret;
  780. unsigned int width, height;
  781. v4l2_subdev_call(sd, video, s_crop, crop);
  782. ret = client_g_rect(sd, cam_rect);
  783. if (ret < 0)
  784. return ret;
  785. /*
  786. * Now cam_crop contains the current camera input rectangle, and it must
  787. * be within camera cropcap bounds
  788. */
  789. if (!memcmp(rect, cam_rect, sizeof(*rect))) {
  790. /* Even if camera S_CROP failed, but camera rectangle matches */
  791. dev_dbg(dev, "Camera S_CROP successful for %ux%u@%u:%u\n",
  792. rect->width, rect->height, rect->left, rect->top);
  793. return 0;
  794. }
  795. /* Try to fix cropping, that camera hasn't managed to set */
  796. dev_geo(dev, "Fix camera S_CROP for %ux%u@%u:%u to %ux%u@%u:%u\n",
  797. cam_rect->width, cam_rect->height,
  798. cam_rect->left, cam_rect->top,
  799. rect->width, rect->height, rect->left, rect->top);
  800. /* We need sensor maximum rectangle */
  801. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  802. if (ret < 0)
  803. return ret;
  804. soc_camera_limit_side(&rect->left, &rect->width, cap.bounds.left, 2,
  805. cap.bounds.width);
  806. soc_camera_limit_side(&rect->top, &rect->height, cap.bounds.top, 4,
  807. cap.bounds.height);
  808. /*
  809. * Popular special case - some cameras can only handle fixed sizes like
  810. * QVGA, VGA,... Take care to avoid infinite loop.
  811. */
  812. width = max(cam_rect->width, 2);
  813. height = max(cam_rect->height, 2);
  814. while (!ret && (is_smaller(cam_rect, rect) ||
  815. is_inside(cam_rect, rect)) &&
  816. (cap.bounds.width > width || cap.bounds.height > height)) {
  817. width *= 2;
  818. height *= 2;
  819. cam_rect->width = width;
  820. cam_rect->height = height;
  821. /*
  822. * We do not know what capabilities the camera has to set up
  823. * left and top borders. We could try to be smarter in iterating
  824. * them, e.g., if camera current left is to the right of the
  825. * target left, set it to the middle point between the current
  826. * left and minimum left. But that would add too much
  827. * complexity: we would have to iterate each border separately.
  828. */
  829. if (cam_rect->left > rect->left)
  830. cam_rect->left = cap.bounds.left;
  831. if (cam_rect->left + cam_rect->width < rect->left + rect->width)
  832. cam_rect->width = rect->left + rect->width -
  833. cam_rect->left;
  834. if (cam_rect->top > rect->top)
  835. cam_rect->top = cap.bounds.top;
  836. if (cam_rect->top + cam_rect->height < rect->top + rect->height)
  837. cam_rect->height = rect->top + rect->height -
  838. cam_rect->top;
  839. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  840. ret = client_g_rect(sd, cam_rect);
  841. dev_geo(dev, "Camera S_CROP %d for %ux%u@%u:%u\n", ret,
  842. cam_rect->width, cam_rect->height,
  843. cam_rect->left, cam_rect->top);
  844. }
  845. /* S_CROP must not modify the rectangle */
  846. if (is_smaller(cam_rect, rect) || is_inside(cam_rect, rect)) {
  847. /*
  848. * The camera failed to configure a suitable cropping,
  849. * we cannot use the current rectangle, set to max
  850. */
  851. *cam_rect = cap.bounds;
  852. v4l2_subdev_call(sd, video, s_crop, cam_crop);
  853. ret = client_g_rect(sd, cam_rect);
  854. dev_geo(dev, "Camera S_CROP %d for max %ux%u@%u:%u\n", ret,
  855. cam_rect->width, cam_rect->height,
  856. cam_rect->left, cam_rect->top);
  857. }
  858. return ret;
  859. }
  860. static int get_camera_scales(struct v4l2_subdev *sd, struct v4l2_rect *rect,
  861. unsigned int *scale_h, unsigned int *scale_v)
  862. {
  863. struct v4l2_format f;
  864. int ret;
  865. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  866. ret = v4l2_subdev_call(sd, video, g_fmt, &f);
  867. if (ret < 0)
  868. return ret;
  869. *scale_h = calc_generic_scale(rect->width, f.fmt.pix.width);
  870. *scale_v = calc_generic_scale(rect->height, f.fmt.pix.height);
  871. return 0;
  872. }
  873. static int get_camera_subwin(struct soc_camera_device *icd,
  874. struct v4l2_rect *cam_subrect,
  875. unsigned int cam_hscale, unsigned int cam_vscale)
  876. {
  877. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  878. struct v4l2_rect *ceu_rect = &cam->ceu_rect;
  879. if (!ceu_rect->width) {
  880. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  881. struct device *dev = icd->dev.parent;
  882. struct v4l2_format f;
  883. struct v4l2_pix_format *pix = &f.fmt.pix;
  884. int ret;
  885. /* First time */
  886. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  887. ret = v4l2_subdev_call(sd, video, g_fmt, &f);
  888. if (ret < 0)
  889. return ret;
  890. dev_geo(dev, "camera fmt %ux%u\n", pix->width, pix->height);
  891. if (pix->width > 2560) {
  892. ceu_rect->width = 2560;
  893. ceu_rect->left = (pix->width - 2560) / 2;
  894. } else {
  895. ceu_rect->width = pix->width;
  896. ceu_rect->left = 0;
  897. }
  898. if (pix->height > 1920) {
  899. ceu_rect->height = 1920;
  900. ceu_rect->top = (pix->height - 1920) / 2;
  901. } else {
  902. ceu_rect->height = pix->height;
  903. ceu_rect->top = 0;
  904. }
  905. dev_geo(dev, "initialised CEU rect %ux%u@%u:%u\n",
  906. ceu_rect->width, ceu_rect->height,
  907. ceu_rect->left, ceu_rect->top);
  908. }
  909. cam_subrect->width = scale_up(ceu_rect->width, cam_hscale);
  910. cam_subrect->left = scale_up(ceu_rect->left, cam_hscale);
  911. cam_subrect->height = scale_up(ceu_rect->height, cam_vscale);
  912. cam_subrect->top = scale_up(ceu_rect->top, cam_vscale);
  913. return 0;
  914. }
  915. static int client_s_fmt(struct soc_camera_device *icd, struct v4l2_format *f,
  916. bool ceu_can_scale)
  917. {
  918. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  919. struct device *dev = icd->dev.parent;
  920. struct v4l2_pix_format *pix = &f->fmt.pix;
  921. unsigned int width = pix->width, height = pix->height, tmp_w, tmp_h;
  922. unsigned int max_width, max_height;
  923. struct v4l2_cropcap cap;
  924. int ret;
  925. cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  926. ret = v4l2_subdev_call(sd, video, cropcap, &cap);
  927. if (ret < 0)
  928. return ret;
  929. max_width = min(cap.bounds.width, 2560);
  930. max_height = min(cap.bounds.height, 1920);
  931. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  932. if (ret < 0)
  933. return ret;
  934. dev_geo(dev, "camera scaled to %ux%u\n", pix->width, pix->height);
  935. if ((width == pix->width && height == pix->height) || !ceu_can_scale)
  936. return 0;
  937. /* Camera set a format, but geometry is not precise, try to improve */
  938. tmp_w = pix->width;
  939. tmp_h = pix->height;
  940. /* width <= max_width && height <= max_height - guaranteed by try_fmt */
  941. while ((width > tmp_w || height > tmp_h) &&
  942. tmp_w < max_width && tmp_h < max_height) {
  943. tmp_w = min(2 * tmp_w, max_width);
  944. tmp_h = min(2 * tmp_h, max_height);
  945. pix->width = tmp_w;
  946. pix->height = tmp_h;
  947. ret = v4l2_subdev_call(sd, video, s_fmt, f);
  948. dev_geo(dev, "Camera scaled to %ux%u\n",
  949. pix->width, pix->height);
  950. if (ret < 0) {
  951. /* This shouldn't happen */
  952. dev_err(dev, "Client failed to set format: %d\n", ret);
  953. return ret;
  954. }
  955. }
  956. return 0;
  957. }
  958. /**
  959. * @rect - camera cropped rectangle
  960. * @sub_rect - CEU cropped rectangle, mapped back to camera input area
  961. * @ceu_rect - on output calculated CEU crop rectangle
  962. */
  963. static int client_scale(struct soc_camera_device *icd, struct v4l2_rect *rect,
  964. struct v4l2_rect *sub_rect, struct v4l2_rect *ceu_rect,
  965. struct v4l2_format *f, bool ceu_can_scale)
  966. {
  967. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  968. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  969. struct device *dev = icd->dev.parent;
  970. struct v4l2_format f_tmp = *f;
  971. struct v4l2_pix_format *pix_tmp = &f_tmp.fmt.pix;
  972. unsigned int scale_h, scale_v;
  973. int ret;
  974. /* 5. Apply iterative camera S_FMT for camera user window. */
  975. ret = client_s_fmt(icd, &f_tmp, ceu_can_scale);
  976. if (ret < 0)
  977. return ret;
  978. dev_geo(dev, "5: camera scaled to %ux%u\n",
  979. pix_tmp->width, pix_tmp->height);
  980. /* 6. Retrieve camera output window (g_fmt) */
  981. /* unneeded - it is already in "f_tmp" */
  982. /* 7. Calculate new camera scales. */
  983. ret = get_camera_scales(sd, rect, &scale_h, &scale_v);
  984. if (ret < 0)
  985. return ret;
  986. dev_geo(dev, "7: camera scales %u:%u\n", scale_h, scale_v);
  987. cam->cam_width = pix_tmp->width;
  988. cam->cam_height = pix_tmp->height;
  989. f->fmt.pix.width = pix_tmp->width;
  990. f->fmt.pix.height = pix_tmp->height;
  991. /*
  992. * 8. Calculate new CEU crop - apply camera scales to previously
  993. * calculated "effective" crop.
  994. */
  995. ceu_rect->left = scale_down(sub_rect->left, scale_h);
  996. ceu_rect->width = scale_down(sub_rect->width, scale_h);
  997. ceu_rect->top = scale_down(sub_rect->top, scale_v);
  998. ceu_rect->height = scale_down(sub_rect->height, scale_v);
  999. dev_geo(dev, "8: new CEU rect %ux%u@%u:%u\n",
  1000. ceu_rect->width, ceu_rect->height,
  1001. ceu_rect->left, ceu_rect->top);
  1002. return 0;
  1003. }
  1004. /* Get combined scales */
  1005. static int get_scales(struct soc_camera_device *icd,
  1006. unsigned int *scale_h, unsigned int *scale_v)
  1007. {
  1008. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1009. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1010. struct v4l2_crop cam_crop;
  1011. unsigned int width_in, height_in;
  1012. int ret;
  1013. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1014. ret = client_g_rect(sd, &cam_crop.c);
  1015. if (ret < 0)
  1016. return ret;
  1017. ret = get_camera_scales(sd, &cam_crop.c, scale_h, scale_v);
  1018. if (ret < 0)
  1019. return ret;
  1020. width_in = scale_up(cam->ceu_rect.width, *scale_h);
  1021. height_in = scale_up(cam->ceu_rect.height, *scale_v);
  1022. *scale_h = calc_generic_scale(width_in, icd->user_width);
  1023. *scale_v = calc_generic_scale(height_in, icd->user_height);
  1024. return 0;
  1025. }
  1026. /*
  1027. * CEU can scale and crop, but we don't want to waste bandwidth and kill the
  1028. * framerate by always requesting the maximum image from the client. See
  1029. * Documentation/video4linux/sh_mobile_camera_ceu.txt for a description of
  1030. * scaling and cropping algorithms and for the meaning of referenced here steps.
  1031. */
  1032. static int sh_mobile_ceu_set_crop(struct soc_camera_device *icd,
  1033. struct v4l2_crop *a)
  1034. {
  1035. struct v4l2_rect *rect = &a->c;
  1036. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1037. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1038. struct v4l2_crop cam_crop;
  1039. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1040. struct v4l2_rect *cam_rect = &cam_crop.c, *ceu_rect = &cam->ceu_rect;
  1041. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1042. struct device *dev = icd->dev.parent;
  1043. struct v4l2_format f;
  1044. struct v4l2_pix_format *pix = &f.fmt.pix;
  1045. unsigned int scale_comb_h, scale_comb_v, scale_ceu_h, scale_ceu_v,
  1046. out_width, out_height;
  1047. u32 capsr, cflcr;
  1048. int ret;
  1049. /* 1. Calculate current combined scales. */
  1050. ret = get_scales(icd, &scale_comb_h, &scale_comb_v);
  1051. if (ret < 0)
  1052. return ret;
  1053. dev_geo(dev, "1: combined scales %u:%u\n", scale_comb_h, scale_comb_v);
  1054. /* 2. Apply iterative camera S_CROP for new input window. */
  1055. ret = client_s_crop(sd, a, &cam_crop);
  1056. if (ret < 0)
  1057. return ret;
  1058. dev_geo(dev, "2: camera cropped to %ux%u@%u:%u\n",
  1059. cam_rect->width, cam_rect->height,
  1060. cam_rect->left, cam_rect->top);
  1061. /* On success cam_crop contains current camera crop */
  1062. /*
  1063. * 3. If old combined scales applied to new crop produce an impossible
  1064. * user window, adjust scales to produce nearest possible window.
  1065. */
  1066. out_width = scale_down(rect->width, scale_comb_h);
  1067. out_height = scale_down(rect->height, scale_comb_v);
  1068. if (out_width > 2560)
  1069. out_width = 2560;
  1070. else if (out_width < 2)
  1071. out_width = 2;
  1072. if (out_height > 1920)
  1073. out_height = 1920;
  1074. else if (out_height < 4)
  1075. out_height = 4;
  1076. dev_geo(dev, "3: Adjusted output %ux%u\n", out_width, out_height);
  1077. /* 4. Use G_CROP to retrieve actual input window: already in cam_crop */
  1078. /*
  1079. * 5. Using actual input window and calculated combined scales calculate
  1080. * camera target output window.
  1081. */
  1082. pix->width = scale_down(cam_rect->width, scale_comb_h);
  1083. pix->height = scale_down(cam_rect->height, scale_comb_v);
  1084. dev_geo(dev, "5: camera target %ux%u\n", pix->width, pix->height);
  1085. /* 6. - 9. */
  1086. pix->pixelformat = cam->camera_fmt->fourcc;
  1087. pix->colorspace = cam->camera_fmt->colorspace;
  1088. capsr = capture_save_reset(pcdev);
  1089. dev_dbg(dev, "CAPSR 0x%x, CFLCR 0x%x\n", capsr, pcdev->cflcr);
  1090. /* Make relative to camera rectangle */
  1091. rect->left -= cam_rect->left;
  1092. rect->top -= cam_rect->top;
  1093. f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1094. ret = client_scale(icd, cam_rect, rect, ceu_rect, &f,
  1095. pcdev->image_mode && !pcdev->is_interlaced);
  1096. dev_geo(dev, "6-9: %d\n", ret);
  1097. /* 10. Use CEU cropping to crop to the new window. */
  1098. sh_mobile_ceu_set_rect(icd, out_width, out_height);
  1099. dev_geo(dev, "10: CEU cropped to %ux%u@%u:%u\n",
  1100. ceu_rect->width, ceu_rect->height,
  1101. ceu_rect->left, ceu_rect->top);
  1102. /*
  1103. * 11. Calculate CEU scales from camera scales from results of (10) and
  1104. * user window from (3)
  1105. */
  1106. scale_ceu_h = calc_scale(ceu_rect->width, &out_width);
  1107. scale_ceu_v = calc_scale(ceu_rect->height, &out_height);
  1108. dev_geo(dev, "11: CEU scales %u:%u\n", scale_ceu_h, scale_ceu_v);
  1109. /* 12. Apply CEU scales. */
  1110. cflcr = scale_ceu_h | (scale_ceu_v << 16);
  1111. if (cflcr != pcdev->cflcr) {
  1112. pcdev->cflcr = cflcr;
  1113. ceu_write(pcdev, CFLCR, cflcr);
  1114. }
  1115. /* Restore capture */
  1116. if (pcdev->active)
  1117. capsr |= 1;
  1118. capture_restore(pcdev, capsr);
  1119. icd->user_width = out_width;
  1120. icd->user_height = out_height;
  1121. /* Even if only camera cropping succeeded */
  1122. return ret;
  1123. }
  1124. /* Similar to set_crop multistage iterative algorithm */
  1125. static int sh_mobile_ceu_set_fmt(struct soc_camera_device *icd,
  1126. struct v4l2_format *f)
  1127. {
  1128. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1129. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1130. struct sh_mobile_ceu_cam *cam = icd->host_priv;
  1131. struct v4l2_pix_format *pix = &f->fmt.pix;
  1132. struct v4l2_format cam_f = *f;
  1133. struct v4l2_pix_format *cam_pix = &cam_f.fmt.pix;
  1134. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1135. struct device *dev = icd->dev.parent;
  1136. __u32 pixfmt = pix->pixelformat;
  1137. const struct soc_camera_format_xlate *xlate;
  1138. struct v4l2_crop cam_crop;
  1139. struct v4l2_rect *cam_rect = &cam_crop.c, cam_subrect, ceu_rect;
  1140. unsigned int scale_cam_h, scale_cam_v;
  1141. u16 scale_v, scale_h;
  1142. int ret;
  1143. bool is_interlaced, image_mode;
  1144. switch (pix->field) {
  1145. case V4L2_FIELD_INTERLACED:
  1146. is_interlaced = true;
  1147. break;
  1148. case V4L2_FIELD_ANY:
  1149. default:
  1150. pix->field = V4L2_FIELD_NONE;
  1151. /* fall-through */
  1152. case V4L2_FIELD_NONE:
  1153. is_interlaced = false;
  1154. break;
  1155. }
  1156. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1157. if (!xlate) {
  1158. dev_warn(dev, "Format %x not found\n", pixfmt);
  1159. return -EINVAL;
  1160. }
  1161. /* 1. Calculate current camera scales. */
  1162. cam_crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1163. ret = client_g_rect(sd, cam_rect);
  1164. if (ret < 0)
  1165. return ret;
  1166. ret = get_camera_scales(sd, cam_rect, &scale_cam_h, &scale_cam_v);
  1167. if (ret < 0)
  1168. return ret;
  1169. dev_geo(dev, "1: camera scales %u:%u\n", scale_cam_h, scale_cam_v);
  1170. /*
  1171. * 2. Calculate "effective" input crop (sensor subwindow) - CEU crop
  1172. * scaled back at current camera scales onto input window.
  1173. */
  1174. ret = get_camera_subwin(icd, &cam_subrect, scale_cam_h, scale_cam_v);
  1175. if (ret < 0)
  1176. return ret;
  1177. dev_geo(dev, "2: subwin %ux%u@%u:%u\n",
  1178. cam_subrect.width, cam_subrect.height,
  1179. cam_subrect.left, cam_subrect.top);
  1180. /*
  1181. * 3. Calculate new combined scales from "effective" input window to
  1182. * requested user window.
  1183. */
  1184. scale_h = calc_generic_scale(cam_subrect.width, pix->width);
  1185. scale_v = calc_generic_scale(cam_subrect.height, pix->height);
  1186. dev_geo(dev, "3: scales %u:%u\n", scale_h, scale_v);
  1187. /*
  1188. * 4. Calculate camera output window by applying combined scales to real
  1189. * input window.
  1190. */
  1191. cam_pix->width = scale_down(cam_rect->width, scale_h);
  1192. cam_pix->height = scale_down(cam_rect->height, scale_v);
  1193. cam_pix->pixelformat = xlate->cam_fmt->fourcc;
  1194. switch (pixfmt) {
  1195. case V4L2_PIX_FMT_NV12:
  1196. case V4L2_PIX_FMT_NV21:
  1197. case V4L2_PIX_FMT_NV16:
  1198. case V4L2_PIX_FMT_NV61:
  1199. image_mode = true;
  1200. break;
  1201. default:
  1202. image_mode = false;
  1203. }
  1204. dev_geo(dev, "4: camera output %ux%u\n",
  1205. cam_pix->width, cam_pix->height);
  1206. /* 5. - 9. */
  1207. ret = client_scale(icd, cam_rect, &cam_subrect, &ceu_rect, &cam_f,
  1208. image_mode && !is_interlaced);
  1209. dev_geo(dev, "5-9: client scale %d\n", ret);
  1210. /* Done with the camera. Now see if we can improve the result */
  1211. dev_dbg(dev, "Camera %d fmt %ux%u, requested %ux%u\n",
  1212. ret, cam_pix->width, cam_pix->height, pix->width, pix->height);
  1213. if (ret < 0)
  1214. return ret;
  1215. /* 10. Use CEU scaling to scale to the requested user window. */
  1216. /* We cannot scale up */
  1217. if (pix->width > cam_pix->width)
  1218. pix->width = cam_pix->width;
  1219. if (pix->width > ceu_rect.width)
  1220. pix->width = ceu_rect.width;
  1221. if (pix->height > cam_pix->height)
  1222. pix->height = cam_pix->height;
  1223. if (pix->height > ceu_rect.height)
  1224. pix->height = ceu_rect.height;
  1225. /* Let's rock: scale pix->{width x height} down to width x height */
  1226. scale_h = calc_scale(ceu_rect.width, &pix->width);
  1227. scale_v = calc_scale(ceu_rect.height, &pix->height);
  1228. dev_geo(dev, "10: W: %u : 0x%x = %u, H: %u : 0x%x = %u\n",
  1229. ceu_rect.width, scale_h, pix->width,
  1230. ceu_rect.height, scale_v, pix->height);
  1231. pcdev->cflcr = scale_h | (scale_v << 16);
  1232. icd->buswidth = xlate->buswidth;
  1233. icd->current_fmt = xlate->host_fmt;
  1234. cam->camera_fmt = xlate->cam_fmt;
  1235. cam->ceu_rect = ceu_rect;
  1236. pcdev->is_interlaced = is_interlaced;
  1237. pcdev->image_mode = image_mode;
  1238. return 0;
  1239. }
  1240. static int sh_mobile_ceu_try_fmt(struct soc_camera_device *icd,
  1241. struct v4l2_format *f)
  1242. {
  1243. const struct soc_camera_format_xlate *xlate;
  1244. struct v4l2_pix_format *pix = &f->fmt.pix;
  1245. struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
  1246. __u32 pixfmt = pix->pixelformat;
  1247. int width, height;
  1248. int ret;
  1249. xlate = soc_camera_xlate_by_fourcc(icd, pixfmt);
  1250. if (!xlate) {
  1251. dev_warn(icd->dev.parent, "Format %x not found\n", pixfmt);
  1252. return -EINVAL;
  1253. }
  1254. /* FIXME: calculate using depth and bus width */
  1255. v4l_bound_align_image(&pix->width, 2, 2560, 1,
  1256. &pix->height, 4, 1920, 2, 0);
  1257. width = pix->width;
  1258. height = pix->height;
  1259. pix->bytesperline = pix->width *
  1260. DIV_ROUND_UP(xlate->host_fmt->depth, 8);
  1261. pix->sizeimage = pix->height * pix->bytesperline;
  1262. pix->pixelformat = xlate->cam_fmt->fourcc;
  1263. /* limit to sensor capabilities */
  1264. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  1265. pix->pixelformat = pixfmt;
  1266. if (ret < 0)
  1267. return ret;
  1268. switch (pixfmt) {
  1269. case V4L2_PIX_FMT_NV12:
  1270. case V4L2_PIX_FMT_NV21:
  1271. case V4L2_PIX_FMT_NV16:
  1272. case V4L2_PIX_FMT_NV61:
  1273. /* FIXME: check against rect_max after converting soc-camera */
  1274. /* We can scale precisely, need a bigger image from camera */
  1275. if (pix->width < width || pix->height < height) {
  1276. int tmp_w = pix->width, tmp_h = pix->height;
  1277. pix->width = 2560;
  1278. pix->height = 1920;
  1279. ret = v4l2_subdev_call(sd, video, try_fmt, f);
  1280. if (ret < 0) {
  1281. /* Shouldn't actually happen... */
  1282. dev_err(icd->dev.parent,
  1283. "FIXME: try_fmt() returned %d\n", ret);
  1284. pix->width = tmp_w;
  1285. pix->height = tmp_h;
  1286. }
  1287. }
  1288. if (pix->width > width)
  1289. pix->width = width;
  1290. if (pix->height > height)
  1291. pix->height = height;
  1292. }
  1293. return ret;
  1294. }
  1295. static int sh_mobile_ceu_reqbufs(struct soc_camera_file *icf,
  1296. struct v4l2_requestbuffers *p)
  1297. {
  1298. int i;
  1299. /*
  1300. * This is for locking debugging only. I removed spinlocks and now I
  1301. * check whether .prepare is ever called on a linked buffer, or whether
  1302. * a dma IRQ can occur for an in-work or unlinked buffer. Until now
  1303. * it hadn't triggered
  1304. */
  1305. for (i = 0; i < p->count; i++) {
  1306. struct sh_mobile_ceu_buffer *buf;
  1307. buf = container_of(icf->vb_vidq.bufs[i],
  1308. struct sh_mobile_ceu_buffer, vb);
  1309. INIT_LIST_HEAD(&buf->vb.queue);
  1310. }
  1311. return 0;
  1312. }
  1313. static unsigned int sh_mobile_ceu_poll(struct file *file, poll_table *pt)
  1314. {
  1315. struct soc_camera_file *icf = file->private_data;
  1316. struct sh_mobile_ceu_buffer *buf;
  1317. buf = list_entry(icf->vb_vidq.stream.next,
  1318. struct sh_mobile_ceu_buffer, vb.stream);
  1319. poll_wait(file, &buf->vb.done, pt);
  1320. if (buf->vb.state == VIDEOBUF_DONE ||
  1321. buf->vb.state == VIDEOBUF_ERROR)
  1322. return POLLIN|POLLRDNORM;
  1323. return 0;
  1324. }
  1325. static int sh_mobile_ceu_querycap(struct soc_camera_host *ici,
  1326. struct v4l2_capability *cap)
  1327. {
  1328. strlcpy(cap->card, "SuperH_Mobile_CEU", sizeof(cap->card));
  1329. cap->version = KERNEL_VERSION(0, 0, 5);
  1330. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1331. return 0;
  1332. }
  1333. static void sh_mobile_ceu_init_videobuf(struct videobuf_queue *q,
  1334. struct soc_camera_device *icd)
  1335. {
  1336. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1337. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1338. videobuf_queue_dma_contig_init(q,
  1339. &sh_mobile_ceu_videobuf_ops,
  1340. icd->dev.parent, &pcdev->lock,
  1341. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  1342. pcdev->is_interlaced ?
  1343. V4L2_FIELD_INTERLACED : V4L2_FIELD_NONE,
  1344. sizeof(struct sh_mobile_ceu_buffer),
  1345. icd);
  1346. }
  1347. static int sh_mobile_ceu_get_ctrl(struct soc_camera_device *icd,
  1348. struct v4l2_control *ctrl)
  1349. {
  1350. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1351. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1352. u32 val;
  1353. switch (ctrl->id) {
  1354. case V4L2_CID_SHARPNESS:
  1355. val = ceu_read(pcdev, CLFCR);
  1356. ctrl->value = val ^ 1;
  1357. return 0;
  1358. }
  1359. return -ENOIOCTLCMD;
  1360. }
  1361. static int sh_mobile_ceu_set_ctrl(struct soc_camera_device *icd,
  1362. struct v4l2_control *ctrl)
  1363. {
  1364. struct soc_camera_host *ici = to_soc_camera_host(icd->dev.parent);
  1365. struct sh_mobile_ceu_dev *pcdev = ici->priv;
  1366. switch (ctrl->id) {
  1367. case V4L2_CID_SHARPNESS:
  1368. switch (icd->current_fmt->fourcc) {
  1369. case V4L2_PIX_FMT_NV12:
  1370. case V4L2_PIX_FMT_NV21:
  1371. case V4L2_PIX_FMT_NV16:
  1372. case V4L2_PIX_FMT_NV61:
  1373. ceu_write(pcdev, CLFCR, !ctrl->value);
  1374. return 0;
  1375. }
  1376. return -EINVAL;
  1377. }
  1378. return -ENOIOCTLCMD;
  1379. }
  1380. static const struct v4l2_queryctrl sh_mobile_ceu_controls[] = {
  1381. {
  1382. .id = V4L2_CID_SHARPNESS,
  1383. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1384. .name = "Low-pass filter",
  1385. .minimum = 0,
  1386. .maximum = 1,
  1387. .step = 1,
  1388. .default_value = 0,
  1389. },
  1390. };
  1391. static struct soc_camera_host_ops sh_mobile_ceu_host_ops = {
  1392. .owner = THIS_MODULE,
  1393. .add = sh_mobile_ceu_add_device,
  1394. .remove = sh_mobile_ceu_remove_device,
  1395. .get_formats = sh_mobile_ceu_get_formats,
  1396. .put_formats = sh_mobile_ceu_put_formats,
  1397. .set_crop = sh_mobile_ceu_set_crop,
  1398. .set_fmt = sh_mobile_ceu_set_fmt,
  1399. .try_fmt = sh_mobile_ceu_try_fmt,
  1400. .set_ctrl = sh_mobile_ceu_set_ctrl,
  1401. .get_ctrl = sh_mobile_ceu_get_ctrl,
  1402. .reqbufs = sh_mobile_ceu_reqbufs,
  1403. .poll = sh_mobile_ceu_poll,
  1404. .querycap = sh_mobile_ceu_querycap,
  1405. .set_bus_param = sh_mobile_ceu_set_bus_param,
  1406. .init_videobuf = sh_mobile_ceu_init_videobuf,
  1407. .controls = sh_mobile_ceu_controls,
  1408. .num_controls = ARRAY_SIZE(sh_mobile_ceu_controls),
  1409. };
  1410. static int __devinit sh_mobile_ceu_probe(struct platform_device *pdev)
  1411. {
  1412. struct sh_mobile_ceu_dev *pcdev;
  1413. struct resource *res;
  1414. void __iomem *base;
  1415. unsigned int irq;
  1416. int err = 0;
  1417. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1418. irq = platform_get_irq(pdev, 0);
  1419. if (!res || !irq) {
  1420. dev_err(&pdev->dev, "Not enough CEU platform resources.\n");
  1421. err = -ENODEV;
  1422. goto exit;
  1423. }
  1424. pcdev = kzalloc(sizeof(*pcdev), GFP_KERNEL);
  1425. if (!pcdev) {
  1426. dev_err(&pdev->dev, "Could not allocate pcdev\n");
  1427. err = -ENOMEM;
  1428. goto exit;
  1429. }
  1430. INIT_LIST_HEAD(&pcdev->capture);
  1431. spin_lock_init(&pcdev->lock);
  1432. pcdev->pdata = pdev->dev.platform_data;
  1433. if (!pcdev->pdata) {
  1434. err = -EINVAL;
  1435. dev_err(&pdev->dev, "CEU platform data not set.\n");
  1436. goto exit_kfree;
  1437. }
  1438. base = ioremap_nocache(res->start, resource_size(res));
  1439. if (!base) {
  1440. err = -ENXIO;
  1441. dev_err(&pdev->dev, "Unable to ioremap CEU registers.\n");
  1442. goto exit_kfree;
  1443. }
  1444. pcdev->irq = irq;
  1445. pcdev->base = base;
  1446. pcdev->video_limit = 0; /* only enabled if second resource exists */
  1447. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1448. if (res) {
  1449. err = dma_declare_coherent_memory(&pdev->dev, res->start,
  1450. res->start,
  1451. resource_size(res),
  1452. DMA_MEMORY_MAP |
  1453. DMA_MEMORY_EXCLUSIVE);
  1454. if (!err) {
  1455. dev_err(&pdev->dev, "Unable to declare CEU memory.\n");
  1456. err = -ENXIO;
  1457. goto exit_iounmap;
  1458. }
  1459. pcdev->video_limit = resource_size(res);
  1460. }
  1461. /* request irq */
  1462. err = request_irq(pcdev->irq, sh_mobile_ceu_irq, IRQF_DISABLED,
  1463. dev_name(&pdev->dev), pcdev);
  1464. if (err) {
  1465. dev_err(&pdev->dev, "Unable to register CEU interrupt.\n");
  1466. goto exit_release_mem;
  1467. }
  1468. pm_suspend_ignore_children(&pdev->dev, true);
  1469. pm_runtime_enable(&pdev->dev);
  1470. pm_runtime_resume(&pdev->dev);
  1471. pcdev->ici.priv = pcdev;
  1472. pcdev->ici.v4l2_dev.dev = &pdev->dev;
  1473. pcdev->ici.nr = pdev->id;
  1474. pcdev->ici.drv_name = dev_name(&pdev->dev);
  1475. pcdev->ici.ops = &sh_mobile_ceu_host_ops;
  1476. err = soc_camera_host_register(&pcdev->ici);
  1477. if (err)
  1478. goto exit_free_clk;
  1479. return 0;
  1480. exit_free_clk:
  1481. pm_runtime_disable(&pdev->dev);
  1482. free_irq(pcdev->irq, pcdev);
  1483. exit_release_mem:
  1484. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1485. dma_release_declared_memory(&pdev->dev);
  1486. exit_iounmap:
  1487. iounmap(base);
  1488. exit_kfree:
  1489. kfree(pcdev);
  1490. exit:
  1491. return err;
  1492. }
  1493. static int __devexit sh_mobile_ceu_remove(struct platform_device *pdev)
  1494. {
  1495. struct soc_camera_host *soc_host = to_soc_camera_host(&pdev->dev);
  1496. struct sh_mobile_ceu_dev *pcdev = container_of(soc_host,
  1497. struct sh_mobile_ceu_dev, ici);
  1498. soc_camera_host_unregister(soc_host);
  1499. pm_runtime_disable(&pdev->dev);
  1500. free_irq(pcdev->irq, pcdev);
  1501. if (platform_get_resource(pdev, IORESOURCE_MEM, 1))
  1502. dma_release_declared_memory(&pdev->dev);
  1503. iounmap(pcdev->base);
  1504. kfree(pcdev);
  1505. return 0;
  1506. }
  1507. static int sh_mobile_ceu_runtime_nop(struct device *dev)
  1508. {
  1509. /* Runtime PM callback shared between ->runtime_suspend()
  1510. * and ->runtime_resume(). Simply returns success.
  1511. *
  1512. * This driver re-initializes all registers after
  1513. * pm_runtime_get_sync() anyway so there is no need
  1514. * to save and restore registers here.
  1515. */
  1516. return 0;
  1517. }
  1518. static const struct dev_pm_ops sh_mobile_ceu_dev_pm_ops = {
  1519. .runtime_suspend = sh_mobile_ceu_runtime_nop,
  1520. .runtime_resume = sh_mobile_ceu_runtime_nop,
  1521. };
  1522. static struct platform_driver sh_mobile_ceu_driver = {
  1523. .driver = {
  1524. .name = "sh_mobile_ceu",
  1525. .pm = &sh_mobile_ceu_dev_pm_ops,
  1526. },
  1527. .probe = sh_mobile_ceu_probe,
  1528. .remove = __devexit_p(sh_mobile_ceu_remove),
  1529. };
  1530. static int __init sh_mobile_ceu_init(void)
  1531. {
  1532. return platform_driver_register(&sh_mobile_ceu_driver);
  1533. }
  1534. static void __exit sh_mobile_ceu_exit(void)
  1535. {
  1536. platform_driver_unregister(&sh_mobile_ceu_driver);
  1537. }
  1538. module_init(sh_mobile_ceu_init);
  1539. module_exit(sh_mobile_ceu_exit);
  1540. MODULE_DESCRIPTION("SuperH Mobile CEU driver");
  1541. MODULE_AUTHOR("Magnus Damm");
  1542. MODULE_LICENSE("GPL");
  1543. MODULE_ALIAS("platform:sh_mobile_ceu");