sh_mobile_ceu_camera.c 54 KB

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