sh_mobile_ceu_camera.c 63 KB

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