sh_mobile_ceu_camera.c 53 KB

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