sh_mobile_ceu_camera.c 61 KB

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