sh_mobile_ceu_camera.c 55 KB

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