sh_mobile_ceu_camera.c 50 KB

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