sh_mobile_ceu_camera.c 58 KB

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