sh_mobile_ceu_camera.c 56 KB

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