sh_mobile_ceu_camera.c 60 KB

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