sh_mobile_ceu_camera.c 56 KB

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