sh_mobile_ceu_camera.c 60 KB

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