sh_mobile_ceu_camera.c 50 KB

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