sh_mobile_ceu_camera.c 62 KB

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